Skip to content

Commit f7f606f

Browse files
committed
new way
1 parent 71eb7bc commit f7f606f

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

playwright/_impl/_transport.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,6 @@ async def connect(self) -> None:
118118
if sys.platform == "win32" and sys.stdout is None:
119119
creationflags = subprocess.CREATE_NO_WINDOW
120120

121-
# In Python 3.7, self._proc.wait() hangs because it does not use ThreadedChildWatcher
122-
# which is used in Python 3.8+. This is unix specific and also takes care about
123-
# cleaning up zombie processes. See https://bugs.python.org/issue35621
124-
if (
125-
sys.version_info[0] == 3
126-
and sys.version_info[1] == 7
127-
and sys.platform != "win32"
128-
and isinstance(asyncio.get_child_watcher(), asyncio.SafeChildWatcher)
129-
):
130-
from ._py37ThreadedChildWatcher import ThreadedChildWatcher # type: ignore
131-
132-
watcher = ThreadedChildWatcher()
133-
asyncio.set_child_watcher(watcher)
134121
try:
135122
# For pyinstaller
136123
env = get_driver_env()

playwright/sync_api/_context_manager.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
# limitations under the License.
1414

1515
import asyncio
16-
from typing import Any
16+
import sys
17+
from typing import Any, Optional
1718

1819
from greenlet import greenlet
1920

@@ -31,6 +32,7 @@ def __init__(self) -> None:
3132
self._playwright: SyncPlaywright
3233
self._loop: asyncio.AbstractEventLoop
3334
self._own_loop = False
35+
self._watcher: Optional[asyncio.AbstractChildWatcher] = None
3436

3537
def __enter__(self) -> SyncPlaywright:
3638
try:
@@ -44,6 +46,20 @@ def __enter__(self) -> SyncPlaywright:
4446
Please use the Async API instead."""
4547
)
4648

49+
# In Python 3.7, asyncio.Process.wait() hangs because it does not use ThreadedChildWatcher
50+
# which is used in Python 3.8+. This is unix specific and also takes care about
51+
# cleaning up zombie processes. See https://bugs.python.org/issue35621
52+
if (
53+
sys.version_info[0] == 3
54+
and sys.version_info[1] == 7
55+
and sys.platform != "win32"
56+
and isinstance(asyncio.get_child_watcher(), asyncio.SafeChildWatcher)
57+
):
58+
from ._py37ThreadedChildWatcher import ThreadedChildWatcher # type: ignore
59+
60+
self._watcher = ThreadedChildWatcher()
61+
asyncio.set_child_watcher(self._watcher) # type: ignore
62+
4763
def greenlet_main() -> None:
4864
self._loop.run_until_complete(self._connection.run_as_sync())
4965

@@ -73,6 +89,8 @@ def start(self) -> SyncPlaywright:
7389

7490
def __exit__(self, *args: Any) -> None:
7591
self._connection.stop_sync()
92+
if self._watcher:
93+
self._watcher.close()
7694
if self._own_loop:
7795
self._loop.run_until_complete(self._loop.shutdown_asyncgens())
7896
self._loop.close()

0 commit comments

Comments
 (0)