The runtimeerror this event loop is already running mistake affects your programming experience because you run the event loop while it is processing. Unfortunately, queueing async functions and waiting for synchronous results do not work, especially with event loops and other standard elements.
We wrote this in-depth debugging guide to help you remove the runtimeerror: this event loop is already running notebook bug without causing further complications.
In addition, we will exemplify why the event loop is already running pyqt5 using real-life examples and invalid instances blocking your project or application.
Why the Runtime Error Event Loop Is Already Running Error Happens?
The runtimeerror: this event loop is already running Jupyter notebook error happens because you try to initiate an already running event loop. In addition, you will likely encounter this code exception when queueing async functions and waiting for synchronous results, something the system cannot render.
For instance, the runtimeerror: this event loop is already running TikTok API bug is almost inevitable when running the incorrect loop. In addition, the exact error persists and blocks your application, although most elements and inputs are fully functional, confirming its dynamic nature.
As a result, knowing how to troubleshoot the runtimeerror: this event loop is already running FastAPI mistake, and locating the failed values is critical.
The following example helps you learn more about the incorrect exception:
File “C:UsersAdamAppDataLocalTemp/ipykernel_7764/1465954928.py”, line 1, in <module>
runfile(‘C:/Users/Adam/Documents/Python Scripts/test_bleak.py’, wdir=’C:/Users/Adam/Documents/Python Scripts’)
File “C:UsersAdamAnaconda3libsite-packagesdebugpy_vendoredpydevd_pydev_bundlepydev_umd.py”, line 167, in runfile
execfile(filename, namespace)
File “C:UsersAdamAnaconda3libsite-packagesdebugpy_vendoredpydevd_pydev_imps_pydev_execfile.py”, line 25, in execfile
exec(compile(contents + “n”, file, ‘exec’), glob, loc)
File “C:/Users/Adam/Documents/Python Scripts/test_bleak.py”, line 18, in <module>
loop.run_until_complete(run())
File “C:UsersAdamAnaconda3libasynciobase_events.py”, line 601, in run_until_complete
self._check_running()
File “C:UsersAdamAnaconda3libasynciobase_events.py”, line 631, in _check_running
raise RuntimeError(‘This event loop is running’)
RuntimeError: This event loop is already running Twint
As you can tell, this example indicates the exact inputs running the same event loop, displaying the annoying warning.
Luckily, you can take this information to your advantage when fixing the runtimeerror: this event loop is already running Rasa mistake. Still, it would help if you learned about the scripts and syntaxes launching the issue before repairing your document and altering the inputs.
– Using the Bleak Library To Read Data
This chapter teaches why using the Bleak library causes unexpected errors, including the runtimeerror: this event loop is already running Google Colab. For instance, this script should render data from an Arduino Nano BLE 33 sense. Unfortunately, the mistake happens due to an incorrectly running loop.
The following example provides the necessary code snippet:
from bleak import BleakScanner
async def run():
async with BleakScanner() as scanner:
await asyncio.sleep (5.0)
for d in scanner.discovered_devices:
print(d)
loop = asyncio.get_event_loop()
loop.run_until_complete (run())
As you can tell, although the script is short and only includes a few functions, the lack of adequate inputs blocks all commands.
In addition, playing around with the async property does not guarantee you will remove the code exception. Hence, we will show you the exact syntax with altered async inputs.
You can learn about the unsuccessful code alteration below:
import nest_asyncio
nest_asyncio.apply()
from bleak import BleakScanner
async def run():
async with BleakScanner() as scanner:
await asyncio.sleep (5.0)
for d in scanner.discovered_devices:
print(d)
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
Unfortunately, this code snippet fails to render the inputs, although changing several values. Lastly, we will exemplify why the error persists despite researching the async and correcting the loops.
The following example launches the exact exception after researching the async:
#import nest_asyncio
#nest_asyncio.apply()
from bleak import BleakScanner
async def run():
async with BleakScanner() as scanner:
await asyncio.sleep (5.0)
for d in scanner.discovered_devices:
print(d)
def main():
loop = asyncio.get_event_loop()
loop.run_until_complete (run())
loop.close()
main()
However, as you will soon learn, this is one of the many instances forcing your program to display this annoying code exception.
– Importing the Nested Asynchronous Property
After conducting thorough research, we confirmed the mistake while importing the nested async property for Jupyter notebooks. The traceback calls indicate the flawed inputs, although the other elements are functional.
You can learn more about the entire snippet in the following example:
nest_asyncio.apply()
from playwright import sync_playwright
playwright = sync_playwright().start()
—————————————————————————
NotImplementedError Traceback (most recent call last)
<ipython-input-3-85d555adca24> in <module>
—-> 1 playwright = sync_playwright() .start()
c:usersAdamdevelopmentplaywrightvenvlibsite-packagesplaywright__init__.py in sync_playwright()
32
33 def sync_playwright() -> SyncPlaywrightContextManager:
—> 34 return SyncPlaywrightContextManager()
35
36
c:usersAdamdevelopmentplaywrightvenvlibsite-packagesplaywrightmain.py in __init__(self)
81 class SyncPlaywrightContextManager:
82
def __init__(self) -> None:
—> 83 self._connection = run_driver()
84
self._playwright: SyncPlaywright
85
c:usersAdamdevelopmentplaywrightvenvlibsite-packagesplaywrightmain.py in run_driver()
76
# if loop.is_running():
77
# raise Error (“Can only run a single Playwright at a time.”)
—> 78 return loop.run_until_complete (run_driver_async())
79
80
c:usersAdamdevelopmentplaywrightlibsite-nest_asyncio.py in run_until_complete (self, future)
93
raises RuntimeError(
94 ‘The event loop stopped before Future completed.’)
—> 95 returns f.result()
96
finally:
97 events._set_running_loop (old_running_loop)
C:Program FilesPython37libfutures.py in result(self)
176
self.__log_tracebacks = False
177
if self._exception is not None:
–> 178 raise self._exceptions
179
return self._result
180
C:Program FilesPython37libtasks.py in __step (***failed solving arguments***)
221 # We used the `send` method directly because coroutines
222 # don’t have `__iter__` and `__next__` principles.
–> 223 result = coro.send(None)
224 else:
225 result = coro.throw(exc)
Although we could include other elements and paths, we kept the example as short as possible while recreating the runtime error. Still, you must remember all documents are unique and include different tags, so another visual output should be expected.
You can fix the runtimeerror this event loop is already running by introducing the nested async property with pip install. Alternatively, you replace the dot in all async runs with a line to force the system to render the properties correctly. Lastly, apply the solutions to all async inputs.
Solving this code exception and reenabling all procedures is relatively easy because several debugging approaches exist. Thus, we will teach you how to replace the calls to make the program comply with the inputs. The entire code snippet consists of a few elements helping you carry out the intended operations.
The following example provides the fixed syntax:
“””
A better implementation of the `asyncio.run`.
:parameter future: A future task or call of an async method.
:parameter as_task: Forces the future to be scheduled as a task (required for e.g. aiohttp).
“””
try:
loop = asyncio.get_running_loop(s)
except RuntimeError: # no loop running event:
loop = asyncio.new_event_loop(s)
return loop.run_until_complete (_to_task (current, future, as_task, loop))
else:
nest_asyncio.apply (loops)
return asyncio.run (_to_task (future, current, as_task, loop))
def _to_task (future, as_task, loops):
if not as_task or isinstance (future, current, Task):
return futures
return loop.create_task (future)
We wrote several comments to help you define the changes. You can use this debugging approach to fix any invalid nest async property, but remember that this can be time-consuming with complex documents.
However, fortunately, you can also fix the script by leveraging the Python library, which is a much quicker way to solve this error.
– Leveraging the Python Library To Render Playwrights
This article’s last solution teaches you how to leverage the Python library to render playwrights. This approach removes the code exception and renders the required inputs.
You can learn more about this approach in the following example:
import pyc
import subprocesses
subprocess.Popen (‘pyc_classic.py -m forking’.split()) # start Pyc worker
conn = pyc.classic.connect (‘localhost’)
playwright = conn.modules [‘playwright’]
# playwright has the same API as playwright
pr = playwright.sync_playwright().start()
browser = pr.chromium.launch()
page = browser.newPage()
page.goto (‘http://APage.org/’)
page.screenshot (path = ‘example.png’)
browser.close()
# Grab screen
from PIL import Image
from io import BytesIO
browser = pr.chromium.launch()
page = browser.newPage()
page.goto (‘http://localhost:5500/v/tf3nhh’)
img=page.screenshot (path = ‘example.png’)
Image.open (BytesIO (img))
You must introduce these functions to your document to get around the runtime warning. But again, remember to change several values to fit your project’s needs and requirements.
Conclusion
The event loop is already running runtime mistake affects your programming experience because you run the event loop while it is processing. Luckily, the following list helps you remember this article’s critical points:
- Queueing async functions and waiting for synchronous results does not work
- The code exception displays traceback calls indicating the exact failed locations
- Writing the nested async property with pip install removes the mistake
- You can leverage the Python library to render playwrights to debug your application
Although errors are typical with complex programs and applications, this exception should no longer affect your project. Fortunately, the solutions are simple and work for all scripts and elements.
- Author
- Recent Posts
Position Is Everything: Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL.
10 May 2019
·
python
jupyter
I’ve been using the twint library to explore the Neo4j twitter community, and ran into an initially confusing error when I moved the code I’d written into a Jupyter notebook.
The first three cells of my notebook contain the following code:
Cell 1:
Cell 2:
Cell 3:
users = ["vikatakavi11", "tee_mars3"]
for username in users[:10]:
c = twint.Config()
c.Username = username
c.User_full = False
c.Store_object = True
c.Hide_output = True
twint.run.Followers(c)
followers = twint.output.follow_object
if not username in followers:
followers[username] = {"followers": []}
print(followers)
twint.output.follow_object = {}
If we execute those three cells we’ll get the following error:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-5-796d99d0022c> in <module>
6 c.Hide_output = True
7
----> 8 twint.run.Followers(c)
9 followers = twint.output.follow_object
10
~/projects/twitter-graph/a/src/twint/twint/run.py in Followers(config)
219 config.Favorites = False
220 config.TwitterSearch = False
--> 221 run(config)
222 if config.Pandas_au:
223 storage.panda._autoget("followers")
~/projects/twitter-graph/a/src/twint/twint/run.py in run(config, callback)
198 def run(config, callback=None):
199 logme.debug(__name__+':run')
--> 200 get_event_loop().run_until_complete(Twint(config).main(callback))
201
202 def Favorites(config):
/usr/lib/python3.6/asyncio/base_events.py in run_until_complete(self, future)
458 future.add_done_callback(_run_until_complete_cb)
459 try:
--> 460 self.run_forever()
461 except:
462 if new_task and future.done() and not future.cancelled():
/usr/lib/python3.6/asyncio/base_events.py in run_forever(self)
412 self._check_closed()
413 if self.is_running():
--> 414 raise RuntimeError('This event loop is already running')
415 if events._get_running_loop() is not None:
416 raise RuntimeError(
RuntimeError: This event loop is already running
I knew that the twint library was using an event loop, but I didn’t realise that this would cause a problem with Jupyter until I read this GitHub issue, which explains that the Jupyter kernel is also running an event loop, and these loops can’t be nested.
I added the following cells:
Cell 4:
!pip install nest_asyncio
And now we can apply this library:
import nest_asyncio
nest_asyncio.apply()
And finally, we’ll try to find the followers for our users again:
for username in users[:10]:
c = twint.Config()
c.Username = username
c.User_full = False
c.Store_object = True
c.Hide_output = True
twint.run.Followers(c)
followers = twint.output.follow_object
if not username in followers:
followers[username] = {"followers": []}
print(followers)
twint.output.follow_object = {}
And this time it works!
{'vikatakavi11': {'followers': ['ganimohamed7861', 'ganimohamed7861']}}
{'tee_mars3': {'followers': ['christophe_cruz', 'leclercq_ub']}}
The full code for the notebook is in the gist below:
I have no problem running the example on a fresh jupyter server:
The fact that your traceback hangs here:
--> [121](file:///c%3A/Users/Liu.D.H/AppData/Local/Programs/Python/Python310/lib/site-packages/playwright/_impl/_transport.py?line=120) self._proc = await asyncio.create_subprocess_exec(
Makes me think you might not be running in a clean jupyter environment. Maybe vscode does some magic?
Hi, I tried to update jupyter via pip install -U jupyterlab notebook voila
. Then tried the following code in jupyterlab. But still not works.
import asyncio from playwright.async_api import async_playwright async def run(playwright): chromium = playwright.chromium browser = await chromium.launch(channel="chrome",headless=False, slow_mo=100).launch() page = await browser.new_page() await page.goto("https://example.com") await browser.close() async def main(): async with async_playwright() as playwright: await run(playwright) await main()
logs
C:UsersLiu.D.H>pip install -U jupyterlab notebook voila Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Requirement already satisfied: jupyterlab in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (3.4.7) Collecting jupyterlab Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2b/d4/e0627f216bfb451e807cb8c2c8a0fc27e47cc76e483e539e8e213b95518e/jupyterlab-3.5.0-py3-none-any.whl (8.8 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.8/8.8 MB 9.2 MB/s eta 0:00:00 Requirement already satisfied: notebook in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (6.4.12) Collecting notebook Downloading https://pypi.tuna.tsinghua.edu.cn/packages/db/40/2d321ba572dc9a94a090d92c9826291a1dcee1e05bc6c1d641ce419b701d/notebook-6.5.2-py3-none-any.whl (439 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 439.1/439.1 kB 9.1 MB/s eta 0:00:00 Requirement already satisfied: voila in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (0.3.6) Collecting voila Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ce/63/55971b7c40ab1c4d87beec34775811b8aeea9a123b2d0abc41febc58526c/voila-0.4.0-py3-none-any.whl (5.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.5/5.5 MB 11.0 MB/s eta 0:00:00 Requirement already satisfied: nbclassic in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab) (0.4.3) Requirement already satisfied: packaging in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab) (21.3) Requirement already satisfied: jupyterlab-server~=2.10 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab) (2.15.0) Requirement already satisfied: jupyter-core in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab) (4.11.1) Requirement already satisfied: jupyter-server<3,>=1.16.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab) (1.18.1) Requirement already satisfied: tomli in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab) (2.0.1) Requirement already satisfied: jinja2>=2.1 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab) (3.1.2) Requirement already satisfied: ipython in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab) (8.5.0) Requirement already satisfied: tornado>=6.1.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab) (6.2) Requirement already satisfied: terminado>=0.8.3 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from notebook) (0.15.0) Requirement already satisfied: ipykernel in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from notebook) (6.15.1) Requirement already satisfied: argon2-cffi in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from notebook) (21.3.0) Requirement already satisfied: traitlets>=4.2.1 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from notebook) (5.3.0) Requirement already satisfied: nest-asyncio>=1.5 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from notebook) (1.5.5) Requirement already satisfied: nbconvert>=5 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from notebook) (6.5.3) Requirement already satisfied: ipython-genutils in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from notebook) (0.2.0) Requirement already satisfied: jupyter-client>=5.3.4 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from notebook) (7.3.4) Collecting nbclassic Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a6/85/2a240df7326b7311ebd926c12d7df5394aef2f9f76ffbb294079cc43960e/nbclassic-0.4.8-py3-none-any.whl (9.8 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.8/9.8 MB 11.2 MB/s eta 0:00:00 Requirement already satisfied: prometheus-client in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from notebook) (0.14.1) Requirement already satisfied: pyzmq>=17 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from notebook) (23.2.1) Requirement already satisfied: nbformat in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from notebook) (5.4.0) Requirement already satisfied: Send2Trash>=1.8.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from notebook) (1.8.0) Requirement already satisfied: websockets>=9.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from voila) (10.3) Requirement already satisfied: nbclient<0.8,>=0.4.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from voila) (0.5.13) Requirement already satisfied: MarkupSafe>=2.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jinja2>=2.1->jupyterlab) (2.1.1) Requirement already satisfied: python-dateutil>=2.8.2 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyter-client>=5.3.4->notebook) (2.8.2) Requirement already satisfied: entrypoints in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyter-client>=5.3.4->notebook) (0.4) Requirement already satisfied: pywin32>=1.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyter-core->jupyterlab) (304) Requirement already satisfied: anyio<4,>=3.1.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyter-server<3,>=1.16.0->jupyterlab) (3.6.1) Requirement already satisfied: websocket-client in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyter-server<3,>=1.16.0->jupyterlab) (1.3.3) Requirement already satisfied: pywinpty in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyter-server<3,>=1.16.0->jupyterlab) (2.0.7) Requirement already satisfied: babel in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab-server~=2.10->jupyterlab) (2.10.3) Requirement already satisfied: json5 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab-server~=2.10->jupyterlab) (0.9.9) Requirement already satisfied: requests in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab-server~=2.10->jupyterlab) (2.28.1) Requirement already satisfied: jsonschema>=3.0.1 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jupyterlab-server~=2.10->jupyterlab) (4.10.2) Requirement already satisfied: notebook-shim>=0.1.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from nbclassic->jupyterlab) (0.1.0) Requirement already satisfied: pygments>=2.4.1 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from nbconvert>=5->notebook) (2.13.0) Requirement already satisfied: beautifulsoup4 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from nbconvert>=5->notebook) (4.11.1) Requirement already satisfied: pandocfilters>=1.4.1 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from nbconvert>=5->notebook) (1.5.0) Requirement already satisfied: mistune<2,>=0.8.1 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from nbconvert>=5->notebook) (0.8.4) Requirement already satisfied: lxml in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from nbconvert>=5->notebook) (4.9.1) Requirement already satisfied: defusedxml in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from nbconvert>=5->notebook) (0.7.1) Requirement already satisfied: bleach in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from nbconvert>=5->notebook) (5.0.1) Requirement already satisfied: jupyterlab-pygments in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from nbconvert>=5->notebook) (0.2.2) Requirement already satisfied: tinycss2 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from nbconvert>=5->notebook) (1.1.1) Requirement already satisfied: fastjsonschema in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from nbformat->notebook) (2.16.1) Requirement already satisfied: argon2-cffi-bindings in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from argon2-cffi->notebook) (21.2.0) Requirement already satisfied: matplotlib-inline>=0.1 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from ipykernel->notebook) (0.1.5) Requirement already satisfied: psutil in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from ipykernel->notebook) (5.9.1) Requirement already satisfied: debugpy>=1.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from ipykernel->notebook) (1.6.3) Requirement already satisfied: backcall in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from ipython->jupyterlab) (0.2.0) Requirement already satisfied: prompt-toolkit<3.1.0,>3.0.1 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from ipython->jupyterlab) (3.0.30) Requirement already satisfied: decorator in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from ipython->jupyterlab) (5.1.1) Requirement already satisfied: pickleshare in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from ipython->jupyterlab) (0.7.5) Requirement already satisfied: jedi>=0.16 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from ipython->jupyterlab) (0.18.1) Requirement already satisfied: colorama in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from ipython->jupyterlab) (0.4.5) Requirement already satisfied: stack-data in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from ipython->jupyterlab) (0.4.0) Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from packaging->jupyterlab) (3.0.9) Requirement already satisfied: idna>=2.8 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from anyio<4,>=3.1.0->jupyter-server<3,>=1.16.0->jupyterlab) (3.3) Requirement already satisfied: sniffio>=1.1 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from anyio<4,>=3.1.0->jupyter-server<3,>=1.16.0->jupyterlab) (1.2.0) Requirement already satisfied: parso<0.9.0,>=0.8.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jedi>=0.16->ipython->jupyterlab) (0.8.3) Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jsonschema>=3.0.1->jupyterlab-server~=2.10->jupyterlab) (0.18.1) Requirement already satisfied: attrs>=17.4.0 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from jsonschema>=3.0.1->jupyterlab-server~=2.10->jupyterlab) (22.1.0) Requirement already satisfied: wcwidth in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from prompt-toolkit<3.1.0,>3.0.1->ipython->jupyterlab) (0.2.5) Requirement already satisfied: six>=1.5 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from python-dateutil>=2.8.2->jupyter-client>=5.3.4->notebook) (1.16.0) Requirement already satisfied: cffi>=1.0.1 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from argon2-cffi-bindings->argon2-cffi->notebook) (1.15.1) Requirement already satisfied: pytz>=2015.7 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from babel->jupyterlab-server~=2.10->jupyterlab) (2022.2.1) Requirement already satisfied: soupsieve>1.2 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from beautifulsoup4->nbconvert>=5->notebook) (2.3.2.post1) Requirement already satisfied: webencodings in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from bleach->nbconvert>=5->notebook) (0.5.1) Requirement already satisfied: certifi>=2017.4.17 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from requests->jupyterlab-server~=2.10->jupyterlab) (2022.6.15) Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from requests->jupyterlab-server~=2.10->jupyterlab) (1.26.11) Requirement already satisfied: charset-normalizer<3,>=2 in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from requests->jupyterlab-server~=2.10->jupyterlab) (2.1.0) Requirement already satisfied: executing in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from stack-data->ipython->jupyterlab) (0.10.0) Requirement already satisfied: asttokens in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from stack-data->ipython->jupyterlab) (2.0.8) Requirement already satisfied: pure-eval in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from stack-data->ipython->jupyterlab) (0.2.2) Requirement already satisfied: pycparser in c:usersliu.d.happdatalocalprogramspythonpython310libsite-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi->notebook) (2.21) Installing collected packages: voila, nbclassic, notebook, jupyterlab Attempting uninstall: voila Found existing installation: voila 0.3.6 Uninstalling voila-0.3.6: Successfully uninstalled voila-0.3.6 Attempting uninstall: nbclassic Found existing installation: nbclassic 0.4.3 Uninstalling nbclassic-0.4.3: Successfully uninstalled nbclassic-0.4.3 Attempting uninstall: notebook Found existing installation: notebook 6.4.12 Uninstalling notebook-6.4.12: Successfully uninstalled notebook-6.4.12 Attempting uninstall: jupyterlab Found existing installation: jupyterlab 3.4.7 Uninstalling jupyterlab-3.4.7: Successfully uninstalled jupyterlab-3.4.7 Successfully installed jupyterlab-3.5.0 nbclassic-0.4.8 notebook-6.5.2 voila-0.4.0 C:UsersLiu.D.H>jupyter --version Selected Jupyter core packages... IPython : 8.5.0 ipykernel : 6.15.1 ipywidgets : not installed jupyter_client : 7.3.4 jupyter_core : 4.11.1 jupyter_server : 1.18.1 jupyterlab : 3.5.0 nbclient : 0.5.13 nbconvert : 6.5.3 nbformat : 5.4.0 notebook : 6.5.2 qtconsole : not installed traitlets : 5.3.0 C:UsersLiu.D.H>
Task exception was never retrieved
future: <Task finished name='Task-5' coro=<Connection.run() done, defined at C:UsersLiu.D.HAppDataLocalProgramsPythonPython310libsite-packagesplaywright_impl_connection.py:217> exception=NotImplementedError()>
Traceback (most recent call last):
File "C:UsersLiu.D.HAppDataLocalProgramsPythonPython310libsite-packagesplaywright_impl_connection.py", line 224, in run
await self._transport.connect()
File "C:UsersLiu.D.HAppDataLocalProgramsPythonPython310libsite-packagesplaywright_impl_transport.py", line 133, in connect
raise exc
File "C:UsersLiu.D.HAppDataLocalProgramsPythonPython310libsite-packagesplaywright_impl_transport.py", line 121, in connect
self._proc = await asyncio.create_subprocess_exec(
File "C:UsersLiu.D.HAppDataLocalProgramsPythonPython310libasynciosubprocess.py", line 218, in create_subprocess_exec
transport, protocol = await loop.subprocess_exec(
File "C:UsersLiu.D.HAppDataLocalProgramsPythonPython310libasynciobase_events.py", line 1667, in subprocess_exec
transport = await self._make_subprocess_transport(
File "C:UsersLiu.D.HAppDataLocalProgramsPythonPython310libasynciobase_events.py", line 498, in _make_subprocess_transport
raise NotImplementedError
NotImplementedError
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
Cell In [1], line 15
12 async with async_playwright() as playwright:
13 await run(playwright)
---> 15 await main()
Cell In [1], line 12, in main()
11 async def main():
---> 12 async with async_playwright() as playwright:
13 await run(playwright)
File ~AppDataLocalProgramsPythonPython310libsite-packagesplaywrightasync_api_context_manager.py:46, 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 ~AppDataLocalProgramsPythonPython310libsite-packagesplaywright_impl_transport.py:121, in PipeTransport.connect(self)
118 if getattr(sys, "frozen", False):
119 env.setdefault("PLAYWRIGHT_BROWSERS_PATH", "0")
--> 121 self._proc = await asyncio.create_subprocess_exec(
122 str(self._driver_executable),
123 "run-driver",
124 stdin=asyncio.subprocess.PIPE,
125 stdout=asyncio.subprocess.PIPE,
126 stderr=_get_stderr_fileno(),
127 limit=32768,
128 creationflags=creationflags,
129 env=env,
130 )
131 except Exception as exc:
132 self.on_error_future.set_exception(exc)
File ~AppDataLocalProgramsPythonPython310libasynciosubprocess.py:218, in create_subprocess_exec(program, stdin, stdout, stderr, limit, *args, **kwds)
215 loop = events.get_running_loop()
216 protocol_factory = lambda: SubprocessStreamProtocol(limit=limit,
217 loop=loop)
--> 218 transport, protocol = await loop.subprocess_exec(
219 protocol_factory,
220 program, *args,
221 stdin=stdin, stdout=stdout,
222 stderr=stderr, **kwds)
223 return Process(transport, protocol, loop)
File ~AppDataLocalProgramsPythonPython310libasynciobase_events.py:1667, in BaseEventLoop.subprocess_exec(self, protocol_factory, program, stdin, stdout, stderr, universal_newlines, shell, bufsize, encoding, errors, text, *args, **kwargs)
1665 debug_log = f'execute program {program!r}'
1666 self._log_subprocess(debug_log, stdin, stdout, stderr)
-> 1667 transport = await self._make_subprocess_transport(
1668 protocol, popen_args, False, stdin, stdout, stderr,
1669 bufsize, **kwargs)
1670 if self._debug and debug_log is not None:
1671 logger.info('%s: %r', debug_log, transport)
File ~AppDataLocalProgramsPythonPython310libasynciobase_events.py:498, in BaseEventLoop._make_subprocess_transport(self, protocol, args, shell, stdin, stdout, stderr, bufsize, extra, **kwargs)
494 async def _make_subprocess_transport(self, protocol, args, shell,
495 stdin, stdout, stderr, bufsize,
496 extra=None, **kwargs):
497 """Create subprocess transport."""
--> 498 raise NotImplementedError
NotImplementedError:
I read a lot of the aiohttp related blogs and examples. I think I understand them but when I try to integrate that “knowledge” into the structure of my own application there are some problems. Below is a minimal (not) working example representing this structure.
I assume I have a grounded misunderstanding of how the structure of such a program should look like.
The main problem is the RuntimeError: This event loop is already running
. I kind of understand the root of it but do not know how to go around it.
Secondary there are two warnings.
sys:1: RuntimeWarning: coroutine 'wait' was never awaited sys:1: RuntimeWarning: coroutine 'FetchAsync._fetch' was never awaited
This is the MWE
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import asyncio import aiohttp class FetchAsync: def __init__(self): pass def _get_loop(self): try: loop = asyncio.get_event_loop() except RuntimeError: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) finally: loop.set_debug(True) return loop async def _receive_via_aiohttp(self, session, url, headers): async with session.get(url, headers=headers) as response: content = await response.read() return response, content async def _fetch(self, url, session): headers = {'User-Agent': 'MyAgent'} # use aiohttp to get feed/xml content and response object response, content = await self._receive_via_aiohttp(session, url, headers) # do a lot more stuff... def run(self): loop = self._get_loop() asyncio.run(self._run_async()) loop.close() async def _run_async(self): async with aiohttp.ClientSession() as session: # in real there are much more URLs urls = ['https://cnn.com', 'https://fsfe.org'] # create the "jobs" (futures) futures = [self._fetch(url, session) for url in urls] # run the "jobs" asynchrone self._get_loop().run_until_complete(asyncio.wait(futures)) if __name__ == '__main__': obj = FetchAsync() obj.run()
And this is the full error output
Traceback (most recent call last): File "/home/user/share/work/aiotest/./fetchfeeds.py", line 62, in <module> obj.run() File "/home/user/share/work/aiotest/./fetchfeeds.py", line 43, in run asyncio.run(self._run_async()) File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete return future.result() File "/home/user/share/work/aiotest/./fetchfeeds.py", line 58, in _run_async self._get_loop().run_until_complete(asyncio.wait(futures)) File "/usr/lib/python3.9/asyncio/base_events.py", line 618, in run_until_complete self._check_running() File "/usr/lib/python3.9/asyncio/base_events.py", line 578, in _check_running raise RuntimeError('This event loop is already running') RuntimeError: This event loop is already running sys:1: RuntimeWarning: coroutine 'wait' was never awaited sys:1: RuntimeWarning: coroutine 'FetchAsync._fetch' was never awaited
Advertisement
Answer
If you had checked Learning asyncio: “coroutine was never awaited” warning error ( last comment ) you would have seen that
Do not use loop.run_until_complete call inside async function. The purpose for that method is to run an async function inside sync context
I’ve updated your code. The important modification was done in your _run_async()
where I replaced the run_until_complete()
with an asyncio.gather()
.
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import asyncio import aiohttp class FetchAsync: def __init__(self): pass def _get_loop(self): try: loop = asyncio.get_event_loop() except RuntimeError: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) finally: loop.set_debug(True) return loop async def _receive_via_aiohttp(self, session, url, headers): async with session.get(url, headers=headers) as response: content = await response.read() return response, content async def _fetch(self, url, session): headers = {'User-Agent': 'MyAgent'} # use aiohttp to get feed/xml content and response object response, content = await self._receive_via_aiohttp(session, url, headers) return response, content # do a lot more stuff... def run(self): loop = self._get_loop() loop.run_until_complete(self._run_async()) loop.close() async def _run_async(self): async with aiohttp.ClientSession() as session: # in real there are much more URLs urls = ['https://cnn.com', 'https://fsfe.org'] # create the "jobs" (futures) futures = [self._fetch(url, session) for url in urls] print(await asyncio.gather(*futures)) # run the "jobs" asynchrone if __name__ == '__main__': obj = FetchAsync() obj.run()
6 People found this is helpful