question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"RuntimeError: This event loop is already running" When attempting REPL example

See original GitHub issue

Each time I attempt the REPl example, I get the traceback below. Sync context-manager example worked though. playwright==0.8.0 REPL Example: https://github.com/microsoft/playwright-python#repl-support-without-context-managers

IPython session

 jake  (e) venvplaywright  ~  ipython
/home/jake/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py:935: UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
  warn("Attempting to work in a virtualenv. If you encounter problems, please "
Python 3.8.3 (default, May 29 2020, 00:00:00) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from playwright import sync_playwright                                                                                                                                                

In [2]: playwright = sync_playwright().start()                                                                                                                                                

Traceback (most recent call last):
  File "/usr/local/bin/ipython", line 11, in <module>
    sys.exit(start_ipython())
  File "/home/jake/.local/lib/python3.8/site-packages/IPython/__init__.py", line 126, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/home/jake/.local/lib/python3.8/site-packages/traitlets/config/application.py", line 664, in launch_instance
    app.start()
  File "/home/jake/.local/lib/python3.8/site-packages/IPython/terminal/ipapp.py", line 356, in start
    self.shell.mainloop()
  File "/home/jake/.local/lib/python3.8/site-packages/IPython/terminal/interactiveshell.py", line 558, in mainloop
    self.interact()
  File "/home/jake/.local/lib/python3.8/site-packages/IPython/terminal/interactiveshell.py", line 541, in interact
    code = self.prompt_for_code()
  File "/home/jake/.local/lib/python3.8/site-packages/IPython/terminal/interactiveshell.py", line 467, in prompt_for_code
    text = self.pt_app.prompt(
  File "/home/jake/.local/lib/python3.8/site-packages/prompt_toolkit/shortcuts/prompt.py", line 994, in prompt
    return self.app.run(set_exception_handler=set_exception_handler)
  File "/home/jake/.local/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 811, in run
    return loop.run_until_complete(
  File "/usr/lib64/python3.8/asyncio/base_events.py", line 592, in run_until_complete
    self._check_running()
  File "/usr/lib64/python3.8/asyncio/base_events.py", line 552, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running

If you suspect this is an IPython 7.13.0 bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@python.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    %config Application.verbose_crash=True

sys:1: RuntimeWarning: coroutine 'Application.run_async' was never awaited

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:40 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
bubthegreatcommented, Mar 8, 2022

It’s been almost two years and this still isn’t fixed that I can find - playwright is explicitly stopping me from using nested loops:

import nest_asyncio
nest_asyncio.apply()
​
from playwright.sync_api import sync_playwright
playwright = sync_playwright().start()
---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
Input In [3], in <cell line: 2>()
      1 from playwright.sync_api import sync_playwright
----> 2 playwright = sync_playwright().start()

File c:\users\bubth\development\soc2\venv\lib\site-packages\playwright\sync_api\_context_manager.py:76, in PlaywrightContextManager.start(self)
     75 def start(self) -> SyncPlaywright:
---> 76     return self.__enter__()

File c:\users\bubth\development\soc2\venv\lib\site-packages\playwright\sync_api\_context_manager.py:42, in PlaywrightContextManager.__enter__(self)
     40             own_loop = loop
     41         if loop.is_running():
---> 42             raise Error(
     43                 """It looks like you are using Playwright Sync API inside the asyncio loop.
     44 Please use the Async API instead."""
     45             )
     47         def greenlet_main() -> None:
     48             loop.run_until_complete(self._connection.run_as_sync())

Error: It looks like you are using Playwright Sync API inside the asyncio loop.
Please use the Async API instead.

The workarounds don’t work for those of us using windows, and they don’t appear to work in WSL either - is there a reason we can’t get more information on why this is still closed two years later with no traction and no functional workarounds for presumably a fairly normal use case of using a jupyter notebook for exploratory programming?

It seems hard to believe that we just assume everyone will use raw iPython for everything so I’m confused by the reluctance to actually fix this.

If I’m understanding the asyncio base_events.py error, it’s because there’s no implementation of the _make_subprocess_transport function - is that not something that would be on the playwright package to implement in it’s async implementation?

5reactions
bubthegreatcommented, Oct 6, 2020

If I get time tonight ill play around with it- I dont see any reason for it not to run in the existing loop - or are there architectural challenges with it sharing the loop?

On Tue, Oct 6, 2020, 8:35 AM Pavel Feldman notifications@github.com wrote:

My understanding is that resolving this requires cooperation between the embedder that controls the loop and sync Playwright that also wants to dispatch its messages forever. And I don’t think such cooperation is the right way to go. Sync API is designed as self-contained, assumes it controls execution. Is there a way to use async in Jupyter?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/microsoft/playwright-python/issues/178#issuecomment-704312511, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5CHIMV52GR76C7R7XFYKTSJMTMLANCNFSM4QLBAFHQ .

Read more comments on GitHub >

github_iconTop Results From Across the Web

RuntimeError: This event loop is already running in python
The loop doesn't return control to the code following the first instance of run_until_complete() until second function completes (thus no code ...
Read more >
Supporting asyncio.get_event_loop().run_until_complete() in ...
The above tries to use get_running_loop() first to fetch the currently running event loop, but if there's not one, it uses get_event_loop() ...
Read more >
no running event loop sys:1: runtimewarning: coroutine 'loop ...
The loop was just created and it's not running yet, therefor the asyncio unable to attach tasks to it. The following example will...
Read more >
How to get around “RuntimeError: This event loop is already ...
Asyncio does not allow its event loop to be nested. So if you are in an environment where the event loop is already...
Read more >
Python Language Summit Lightning Talks, Part 1
This call raises “RuntimeError: This event loop is already running.” As Fried explained, any call to run_until_complete() in a call chain ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found