Skip to content

[Bug]: AsyncPlaywright(next(iter(done)).result()) throws NotImplementedError in jupyter notebook #2720

@datoslabs

Description

@datoslabs

Version

1.49.1

Steps to reproduce

Run the following in jupyter notebook:

import asyncio
import nest_asyncio
nest_asyncio.apply()
from playwright.async_api import async_playwright

asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())

async def test_browser():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()
        await page.goto('https://example.com')
        print(f'Title: {await page.title()}')
        await browser.close()

print(type(asyncio.get_event_loop_policy()))
await test_browser()

Please note, NotImplementedError only occurs inside Jupyter notebook; equivalent code running in python cli works fine.

Expected behavior

Expected output:

<class 'asyncio.windows_events.WindowsProactorEventLoopPolicy'>
Title: Example Domain

Actual behavior

Generated NotImplementedError, output as follows:

Output/trace:

<class 'asyncio.windows_events.WindowsProactorEventLoopPolicy'>
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
Cell In[2], line 22
     19         await browser.close()
     21 print(type(asyncio.get_event_loop_policy()))
---> 22 await test_browser()

Cell In[2], line 14, in test_browser()
     13 async def test_browser():
---> 14     async with async_playwright() as p:
     15         browser = await p.chromium.launch(headless=True)
     16         page = await browser.new_page()

File [d:\Users\xyz\.conda\envs\ai-crawler-3.11\Lib\site-packages\playwright\async_api\_context_manager.py:46](file:///D:/Users/xyz/.conda/envs/ai-crawler-3.11/Lib/site-packages/playwright/async_api/_context_manager.py#line=45), in PlaywrightContextManager.__aenter__(self)
     44 if not playwright_future.done():
     45     playwright_future.cancel()
---> 46 playwright = AsyncPlaywright(next(iter(done)).result())
     47 playwright.stop = self.__aexit__  # type: ignore
     48 return playwright

File [d:\Users\xyz\.conda\envs\ai-crawler-3.11\Lib\asyncio\futures.py:203](file:///D:/Users/xyz/.conda/envs/ai-crawler-3.11/Lib/asyncio/futures.py#line=202), in Future.result(self)
    201 self.__log_traceback = False
    202 if self._exception is not None:
--> 203     raise self._exception.with_traceback(self._exception_tb)
    204 return self._result

File [d:\Users\xyz\.conda\envs\ai-crawler-3.11\Lib\site-packages\playwright\_impl\_transport.py:120](file:///D:/Users/xyz/.conda/envs/ai-crawler-3.11/Lib/site-packages/playwright/_impl/_transport.py#line=119), in PipeTransport.connect(self)
    117         startupinfo.wShowWindow = subprocess.SW_HIDE
    119     executable_path, entrypoint_path = compute_driver_executable()
--> 120     self._proc = await asyncio.create_subprocess_exec(
    121         executable_path,
    122         entrypoint_path,
    123         "run-driver",
    124         stdin=asyncio.subprocess.PIPE,
    125         stdout=asyncio.subprocess.PIPE,
    126         stderr=_get_stderr_fileno(),
    127         limit=32768,
    128         env=env,
    129         startupinfo=startupinfo,
    130     )
    131 except Exception as exc:
    132     self.on_error_future.set_exception(exc)

File [d:\Users\xyz\.conda\envs\ai-crawler-3.11\Lib\asyncio\subprocess.py:223](file:///D:/Users/xyz/.conda/envs/ai-crawler-3.11/Lib/asyncio/subprocess.py#line=222), in create_subprocess_exec(program, stdin, stdout, stderr, limit, *args, **kwds)
    220 loop = events.get_running_loop()
    221 protocol_factory = lambda: SubprocessStreamProtocol(limit=limit,
    222                                                     loop=loop)
--> 223 transport, protocol = await loop.subprocess_exec(
    224     protocol_factory,
    225     program, *args,
    226     stdin=stdin, stdout=stdout,
    227     stderr=stderr, **kwds)
    228 return Process(transport, protocol, loop)

File [d:\Users\xyz\.conda\envs\ai-crawler-3.11\Lib\asyncio\base_events.py:1708](file:///D:/Users/xyz/.conda/envs/ai-crawler-3.11/Lib/asyncio/base_events.py#line=1707), in BaseEventLoop.subprocess_exec(self, protocol_factory, program, stdin, stdout, stderr, universal_newlines, shell, bufsize, encoding, errors, text, *args, **kwargs)
   1706     debug_log = f'execute program {program!r}'
   1707     self._log_subprocess(debug_log, stdin, stdout, stderr)
-> 1708 transport = await self._make_subprocess_transport(
   1709     protocol, popen_args, False, stdin, stdout, stderr,
   1710     bufsize, **kwargs)
   1711 if self._debug and debug_log is not None:
   1712     logger.info('%s: %r', debug_log, transport)

File [d:\Users\xyz\.conda\envs\ai-crawler-3.11\Lib\asyncio\base_events.py:503](file:///D:/Users/xyz/.conda/envs/ai-crawler-3.11/Lib/asyncio/base_events.py#line=502), in BaseEventLoop._make_subprocess_transport(self, protocol, args, shell, stdin, stdout, stderr, bufsize, extra, **kwargs)
    499 async def _make_subprocess_transport(self, protocol, args, shell,
    500                                      stdin, stdout, stderr, bufsize,
    501                                      extra=None, **kwargs):
    502     """Create subprocess transport."""
--> 503     raise NotImplementedError

NotImplementedError:

Additional context

No response

Environment

Python 3.11.11

- Operating System: Windows 11 Home Version 10.0.26120 Build 26120
- CPU: [core i7]
- Browser: [All, Chromium, Firefox, WebKit]
- Python Version: [3.11.11]
- Other info:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions