Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Create new event loop if there isn't one running #61

Closed
FlyingSamson opened this issue Nov 30, 2021 · 6 comments
Closed

Create new event loop if there isn't one running #61

FlyingSamson opened this issue Nov 30, 2021 · 6 comments

Comments

@FlyingSamson
Copy link

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 call asyncio.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 #52

RuntimeError: There is no current event loop in thread 'Thread-2'.

due 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 existing asyncio.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.

@erdewit
Copy link
Owner

erdewit commented Dec 1, 2021

It seems anyio does not call asyncio.run, but calls it's own copy-pasted version of it:

https://github.com/agronholm/anyio/blob/cb5b64cfb31af68325d94c591a04d16bae2a8d70/src/anyio/_backends/_asyncio.py#L61

@FlyingSamson
Copy link
Author

Thank you for your fast reply.

Yes, it does, but only for Python versions up until 3.7. Starting with 3.7 native_run is but an alias of asyncio.run:
https://github.com/agronholm/anyio/blob/cb5b64cfb31af68325d94c591a04d16bae2a8d70/src/anyio/_backends/_asyncio.py#L51

So until 3.7 anyio would probably spawn new loops if asked to run on a new Thread but not for more recent versions.

@erdewit
Copy link
Owner

erdewit commented Dec 1, 2021

Would this commit work for your use case?

1856573

@FlyingSamson
Copy link
Author

Yes, this solves the problem perfectly. Thank you very much for the fast help! :)

@erdewit
Copy link
Owner

erdewit commented Dec 1, 2021

Okay. This is released now in v1.5.2.

@FlyingSamson
Copy link
Author

Great! Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants