-
Notifications
You must be signed in to change notification settings - Fork 296
Comms with async functions #868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @mlucool, you could use a pattern like the following to avoid using
The above will only work if there is an asyncio loop running, which is the case when run in Assuming python/cpython#98440 gets merged instead of python/cpython#93453, this pattern will continue to work in Python 3.12+. |
@blink1073 python/cpython#98440 made no such changes to what happens with get_event_loop when an event loop is already running. You only need to use |
If I understand correctly, |
No, I just tried Python 3.11.1 which has the backport. As expected, ---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[6], line 1
----> 1 asyncio.create_task(foo())
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/tasks.py:371, in create_task(coro, name, context)
366 def create_task(coro, *, name=None, context=None):
367 """Schedule the execution of a coroutine object in a spawn task.
368
369 Return a Task object.
370 """
--> 371 loop = events.get_running_loop()
372 if context is None:
373 # Use legacy API if context is not needed
374 task = loop.create_task(coro)
RuntimeError: no running event loop However, using the following still works and does not give a warning: In [7]: loop = asyncio.get_event_loop()
...: loop.create_task(foo())
Out[7]: <Task pending name='Task-436' coro=<foo() running at <ipython-input-5-f3ec22a30cd8>:1>> |
I get the same error with |
Hi,
Is there a recommendation for when a comm needs to call an async function. While support for #433 would be ideal, the usecase is that there is some
async def foo()
that needs to be called from within a comm. For now, the best way to work around this was to make it synchronous via (using ideas from ipython/ipython#13670 (comment)):This seems to work, but I didn't know if there was a better way.
The text was updated successfully, but these errors were encountered: