Skip to content

Conversation

@kristjanvalur
Copy link
Contributor

asyncio.run is the preferred way to run an async application.
It handles all the bits, such as constructing and setting up the event loop, running it and closing it.
It also takes care of clean shutdown, i.e. cancelling remaining Tasks and shutting down async generators.

This PR modifies the examples to the simpler syntax of using asyncio.run(), and adds some unit tests.

@johnzhou721
Copy link
Contributor

By this point, might as well use qasync.run instead.

@kristjanvalur
Copy link
Contributor Author

bah, loop_factory parameter was only added in 3.12

@kristjanvalur
Copy link
Contributor Author

By this point, might as well use qasync.run instead.

You are right, only for latest python, using asyncio.run(loop_factory) is the preferred way and policies are being deprecated.
At any rate, using asyncio.run(), even via qasync.run(), should be the recommended way in the examples, since it performs all the necessary boilerplate and cleanup.

@johnzhou721
Copy link
Contributor

@kristjanvalur qasync.run will be fixed up to remove policies. Refs #131; the plan is to hold that until pi-thon (pun intended, \pi\approx3.14) is official.

@johnzhou721
Copy link
Contributor

@kristjanvalur So... since supporting 3.8 is still existing, my 2 cents are to close this, document qasync.run for now, then watch #131.

@johnzhou721
Copy link
Contributor

For posterity: #131 is replaced by #140.

@johnzhou721
Copy link
Contributor

Hmm… I see that you’ve restricted the version to make sure it works correctly — I think that’s good and I take back my comment for closing this. Qasync.run testing is in #134 though…

@hosaka
Copy link
Collaborator

hosaka commented Jul 24, 2025

@kristjanvalur Thanks for adding this. I wasn't aware of loop_factory and looks like it's the recommended way to configure a custom event loop. I've changed the examples to use asyncio.run instead, same as your change to the readme.

@hosaka hosaka mentioned this pull request Jul 24, 2025
@hosaka hosaka merged commit 065ac98 into CabbageDevelopment:master Jul 24, 2025
72 of 73 checks passed
@kristjanvalur
Copy link
Contributor Author

@kristjanvalur Thanks for adding this. I wasn't aware of loop_factory and looks like it's the recommended way to configure a custom event loop.

Yeah, except that it wasn't added until version 3.12. I guess qasync.run() could be fixed to accept that kw argument and either pass it on, or construct a policy depending on python version. I could cook something up if you are interested.

@kristjanvalur kristjanvalur deleted the asyncio-run branch July 24, 2025 10:02
@hosaka
Copy link
Collaborator

hosaka commented Jul 24, 2025

I think this will be covered in #140, I've posted a simple change that covers both pre and post 3.12 that doesn't break qasync.run.

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.

3 participants