This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 81
Create new event loop if there isn't one running #61
Comments
It seems anyio does not call |
Thank you for your fast reply. Yes, it does, but only for Python versions up until 3.7. Starting with 3.7 So until 3.7 anyio would probably spawn new loops if asked to |
erdewit
added a commit
that referenced
this issue
Dec 1, 2021
Would this commit work for your use case? |
Yes, this solves the problem perfectly. Thank you very much for the fast help! :) |
Okay. This is released now in v1.5.2. |
Great! Thank you! |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
On a similar note to #52, I was wondering if it were possible/desirable to have the patched
run
method fall back to_run_orig
if there is no event loop running on the current thread it is executed on. This would allow to create a new event loop in that case rather than throwing an exception telling me that there is currently no event loop running on that thread.Some background on the situation in which I wished for this behavior:
I'm currently trying to contribute to the jupyter-lsp project by implementing TCP support. However, it was desired to implement this functionality using Anyio rather than Asyncio. Now, within a synchronous function (which gets called on a thread where there is already an event loop running) I'd like to spawn a new thread running a new event loop in the background by creating a
Thread(target=anyio.run, kwargs={'func': <my coro to run in parallel>})
. But if I start that Thread,anyio.run
will callasyncio.run
which at that moment has already been patched somewhere in the jupyter code. The patched version then fails with the same exception as was already reported in #52due to there not being a running event loop on the newly created thread. Moreover, through the interface provided by anyio I am not able to create a new event loop manually (at least not without explicitly using asyncio code) in the new thread.
So I thought maybe it would be more intuitive, if
nest_asyncio
would merely add functionality to the existingasyncio.run
method by allowing it to be called in a context where there is already an event loop running and, at the same time, would still keep the existing functionality of creating an event loop if there isn't one running yet. Maybe I'm just missing an important aspect, why this would be a bad idea?I would, of course, be more than happy to provide an MR if you also would consider this a good idea.
The text was updated successfully, but these errors were encountered: