Asyncer, async and await, focused on developer experience.
Documentation:https://asyncer.tiangolo.com
Source Code:https://github.com/fastapi/asyncer
Asynceris a small library built on top ofAnyIO.
Asyncerhas a small number of utility functions that allow working withasync
,await
,and concurrent code in a more convenient way under my (@tiangolo - Sebastián Ramírez) very opinionated and subjective point of view.
The main goal ofAsynceris to improvedeveloper experienceby providing better support forautocompletionandinline errorsin the editor, andmore certaintythat the code isbug-freeby providing better support for type checking tools likemypy.
Asynceralso tries to improveconvenienceand simplicity when working withasynccodemixedwith regularblocking code,allowing to use them together in a simpler way... again, under my verysubjectivepoint of view.
This small library only exists to be able to use theseutility functionsuntil (and if) they are integrated intoAnyIO.
It will probably take some time for that to happen (or to be decided if it will be included or not).
So I made this to be able to use these ideas right now. 🤓
Yes 🎉 (but continue reading).
You can use this and evaluate thelibrary API designI'm proposing. It will probably be useful to know if it works and is useful for you (I hope so).
But still, consider this lab material, expect it to change a bit. 🧪
If you use it,pin the exact Asyncer versionfor your project, to make sure it all works.
Havetestsfor your project (as you should, anyway). Andupgrade the versiononce you know that the new version continues to work correctly.
Still, it'sjust 4 functions,so there's not much to change, if you had to refactor your code to update something it would not be much.
And if you don't want to addasyncer
as a dependency to your project, you can also just copy the main file and try out those functions, it's quite small (but in that case you won't get updates easily).
AsAsynceris based onAnyIOit will be also installed automatically when you installAsyncer.
$pip install asyncer
---> 100%
Successfully installed asyncer anyio
You can read more about each of the use cases and utility functions inAsyncerin thetutorial.
As a sneak preview of one of the utilities, you cancall sync code from async codeusingasyncify()
:
importtime
importanyio
fromasyncerimportasyncify
defdo_sync_work(name:str):
time.sleep(1)
returnf "Hello,{name}"
asyncdefmain():
message=awaitasyncify(do_sync_work)(name="World")
print(message)
anyio.run(main)
Asyncer'sasyncify()
will use AnyIO underneath to dothe smart thing,avoid blocking the mainasyncevent loop, and run thesync/blocking function in aworker thread.
Everything inAsynceris designed to get the bestdeveloper experiencepossible, with the best editor support.
- Autocompletionfor function arguments:
- Autocompletionfor return values:
- Inline errorsin editor:
- Support for tools likemypy,that can help you verify that yourcode is correct,and prevent many bugs.
This project is licensed under the terms of theMIT license.