Allows call await from synchronous context
Project description
colorblind-async
Warning This is only a proof-of-concept.
Usage
- import
ColorblindAsync
fromcolorblind_async
- init by:
ColorblindAsync.init()
- include files/modules with awaits outside async context
- run
Example
main.py
from colorblind_async import ColorblindAsync
ColorblindAsync.init()
import file # now when you include other modules, the colorblind_async will do it's job
# await my_coro() # you cannot do that here, colorblind_async binds to import statement and it won't work here
file.py
async def my_coro():
return "foo"
def my_sync_fn():
await my_coro() # call `await` in sync function
await my_coro() # I can even call `await` outside any function
Install
pip3 install colorblind-async
Optional setup
Pass already defined event loop
from colorblind_async import ColorblindAsync
ColorblindAsync.init(loop=my_loop)
Debug
Prints altered code with await
s outside async context replaced with __colorblind_async__(coro)
call.
DEBUG_COLORBLIND_ASYNC=True python script.py
Run Tests
make test
How does it work
After you import colorblind_async
module, it'll enhance the include
function to catch SyntaxError
await outside function...
. When it catches it, it parse AST of the included file and replace await
keyword with call __colorblind_async__(coroutine)
. __colorblind_async__
is simple wrapper which executes passed coroutine in the (provided) event loop.
Because you can call sync functions from async context and then call async context in a nested fashion, library nest-asyncio
is used for reentrant async semantics.
Limitations
As per library nest-asyncio
: only event loops from asyncio can be patched; Loops from other projects, such as uvloop or quamash, generally can't be patched.
Be aware of unexpected preemptions as they may occur outside of visible async
keyword context and you may need to apply additional synchronization.
FAQ
Why?
Python async is more complicated than it needs to be. And I like the idea of Zig's colorblind Async/Await. Obviously there are cases when you do not care about event loop stuff and just what the code running without spending too much time on dealing with sync/async parts calling each other.
Is this dark magic?
Well, um, technically yes. Because it allows you to import syntactically incorrect program, so use it with caution. But don't worry, no kittens were harmed during the experiments.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file colorblind-async-0.0.1.tar.gz
.
File metadata
- Download URL: colorblind-async-0.0.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd277fcead9ac36aa75b8cd95d2529941c676006024da8f57521ab640f3dfc6b |
|
MD5 | d0e14a4673f2914eb98f47d49bb34f1c |
|
BLAKE2b-256 | 9b022703ca3ce09010188d6cd375fc45be98d7ab0291b32c6cadf2bde140ba9d |
File details
Details for the file colorblind_async-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: colorblind_async-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | da23a03677c43d8ef5f53f0ad317a7949611b294e12aaf928bf5975e43fcf05a |
|
MD5 | dc9605dc683311a97cfda1caa47ce0fc |
|
BLAKE2b-256 | a646c0468375ad0c5a63b807a82a0ee4177518be6a20ea963a18ce3426a60b0b |