And yeah, Python's documentation is useless. Never how to use stuff, only listing of everything that's possible to do / the API. Unfortunately that style is being mimicked by most other Python projects as well.
The Python project just don’t make it obvious how to do it easy.
import asyncio
def background(f):
def wrapped(*args, **kwargs):
return asyncio.get_event_loop().run_in_executor(None, f, *args, **kwargs)
return wrapped
@background
def my_io_bound_function():...Things I've found particularly useful:
* Optional / debug logging of coroutine start/exit.
* Stats logging, including count, runtime, request queue (multiple instances of the same function) depth.
* Printing tracebacks within the coroutine context.
Haven't expended any effort on decorators, good for you.
I've found random slowdowns of a factor of 10 for no reason.
A context manager needs to have __enter__ and __exit__ methods. An async context manager needs to have __aenter__ and __aexit__ methods. The error you get if you use a context manager as an async context manager or vice versa is big and scary and offers no clue to most programmers of what the problem is.
Using async with SQLAlchemy it isn't hard to wind up exiting the event loop before all cleanup has happened. They fixed the main cause of this, but it isn't hard to still trigger it.
And so on.