Skip to content

Commit 2df2769

Browse files
committed
Move all process functions to new threadpool executor.
1 parent 87c90c1 commit 2df2769

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

choreographer/browser_async.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ async def close(self) -> None:
4646
class Browser(Target):
4747
"""`Browser` is the async implementation of `Browser`."""
4848

49+
subprocess: subprocess.Popen[bytes] | subprocess.Popen[str]
50+
"""A reference to the `Popen` object."""
51+
4952
tabs: MutableMapping[str, Tab]
5053
"""A mapping by target_id of all the targets which are open tabs."""
5154
targets: MutableMapping[str, Target]
@@ -96,7 +99,7 @@ def __init__(
9699
"""
97100
_logger.debug("Attempting to open new browser.")
98101

99-
self._check_closed_executor = _manual_thread_pool.ManualThreadExecutor(
102+
self._process_executor = _manual_thread_pool.ManualThreadExecutor(
100103
max_workers=3,
101104
name="checking_close",
102105
)
@@ -144,7 +147,10 @@ def run() -> subprocess.Popen[bytes] | subprocess.Popen[str]: # depends on args
144147

145148
_logger.debug("Trying to open browser.")
146149
loop = asyncio.get_running_loop()
147-
self.subprocess = await loop.run_in_executor(None, run)
150+
self.subprocess = await loop.run_in_executor(
151+
self._process_executor,
152+
run,
153+
)
148154

149155
super().__init__("0", self._broker)
150156
self._add_session(Session("", self._broker))
@@ -194,7 +200,7 @@ async def _is_closed(self, wait: int | None = 0) -> bool:
194200
loop = asyncio.get_running_loop()
195201

196202
await loop.run_in_executor(
197-
self._check_closed_executor,
203+
self._process_executor,
198204
self.subprocess.wait,
199205
wait,
200206
)
@@ -246,6 +252,8 @@ async def close(self) -> None:
246252
self._watch_dog_task.cancel()
247253
if not self._release_lock():
248254
return
255+
# it can never be mid open here, because all of these must
256+
# run on the same thread. Do not push open or close to threads.
249257
try:
250258
_logger.debug("Starting browser close methods.")
251259
await self._close()
@@ -263,7 +271,7 @@ async def close(self) -> None:
263271
_logger.debug("Browser channel closed.")
264272
self._browser_impl.clean() # os blocky/hangy across networks
265273
_logger.debug("Browser implementation cleaned up.")
266-
self._check_closed_executor.shutdown(wait=False, cancel_futures=True)
274+
self._process_executor.shutdown(wait=False, cancel_futures=True)
267275

268276
async def __aexit__(
269277
self,

0 commit comments

Comments
 (0)