- Notifications
You must be signed in to change notification settings - Fork67
Asyncer, async and await, focused on developer experience.
License
fastapi/asyncer
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Asyncer, async and await, focused on developer experience.
Documentation:https://asyncer.tiangolo.com
Source Code:https://github.com/fastapi/asyncer
Asyncer is a small library built on top ofAnyIO.
Asyncer has 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 ofAsyncer is to improvedeveloper experience by providing better support forautocompletion andinline errors in the editor, andmore certainty that the code isbug-free by providing better support for type checking tools likemypy.
Asyncer also tries to improveconvenience and simplicity when working withasync codemixed with regularblocking code, allowing to use them together in a simpler way... again, under my verysubjective point of view.
This small library only exists to be able to use theseutility functions until (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 design I'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 version for your project, to make sure it all works.
Havetests for your project (as you should, anyway). Andupgrade the version once 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).
AsAsyncer is based onAnyIO it 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 inAsyncer in thetutorial.
As a sneak preview of one of the utilities, you cancall sync code from async code usingasyncify()
:
importtimeimportanyiofromasyncerimportasyncifydefdo_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 mainasync event loop, and run thesync/blocking function in aworker thread.
Everything inAsyncer is designed to get the bestdeveloper experience possible, with the best editor support.
- Autocompletion for function arguments:
- Autocompletion for return values:
- Inline errors in 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.
About
Asyncer, async and await, focused on developer experience.