Skip to content

Add support for other event loops - #1

Merged
tlambert03 merged 5 commits into
tlambert03:support-asyncfrom
davidbrochart:support-async
Feb 17, 2025
Merged

Add support for other event loops#1
tlambert03 merged 5 commits into
tlambert03:support-asyncfrom
davidbrochart:support-async

Conversation

@davidbrochart

Copy link
Copy Markdown

Sorry for the long silence @tlambert03. This is what I have in mind for supporting other event loops than asyncio. Right now it only adds support for AnyIO, but we could add Trio too (if people want to use Trio not through AnyIO).
Basically, users have to call set_async_backend() with the backend of their choice ("asyncio" or "anyio"). This should be done as early as possible, and only once. It means this PR only supports one global event loop, which I think is good enough, but we could improve it to support multiple event loops in the future.
Then users must launch the get_async_backend().run coroutine in a background task. This part is event-loop specific. For asyncio, that would be asyncio.create_task(get_async_backend().run()). For AnyIO, that would be tg.start_soon(get_async_backend().run), supposing they got a task group tg somehow.
Let me know what you think. The PR is not polished but this is the idea.

@davidbrochart
davidbrochart marked this pull request as draft February 16, 2025 22:01
@tlambert03

Copy link
Copy Markdown
Owner

thank you @davidbrochart! I think this is great.

users must launch the get_async_backend().run coroutine in a background task. This part is event-loop specific. For asyncio, that would be asyncio.create_task(get_async_backend().run()). For AnyIO, that would be tg.start_soon(get_async_backend().run), supposing they got a task group tg somehow.
Let me know what you think.

that's fine with me, it's similar (in spirit) to the start_emitting_from_queue convenience that we provide for those wanting to use asynchronous psygnal emission in a Qt threads context.

Basically, users have to call set_async_backend() with the backend of their choice ("asyncio" or "anyio"). This should be done as early as possible, and only once.

Also works for me. I also wouldn't be opposed to depending on or vendoring the tiny sniffio if you think it would generally just do the right thing without requiring the psygnal user to have to call set_async_backend?


Thanks a bunch for this pattern. I'm happy to merge it and polish it up with some tests and ping you for feedback (unless you want to do that yourself and make a PR?)

@davidbrochart

Copy link
Copy Markdown
Author

I also wouldn't be opposed to depending on or vendoring the tiny sniffio if you think it would generally just do the right thing without requiring the psygnal user to have to call set_async_backend?

Hmm I don't think that would work, because we would not detect AnyIO, which doesn't have an event loop per say (it itself uses a Trio or an asyncio backend), yet AnyIO's API is different than Trio's.

I'm happy to merge it and polish it up with some tests and ping you for feedback

Thanks, that works for me 😄

@davidbrochart
davidbrochart marked this pull request as ready for review February 17, 2025 13:23
@tlambert03

Copy link
Copy Markdown
Owner

ok sounds good!

@tlambert03
tlambert03 merged commit a3d1c7f into tlambert03:support-async Feb 17, 2025
@davidbrochart
davidbrochart deleted the support-async branch February 17, 2025 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants