This is a small replacement for the standard (Python 3.5+) await
keyword that you can use when you're inside of a greenlet (and not a Python coroutine).
If you can, of course go with a coroutine. But sometimes it's just not your prerogative.
For example, guys at uWSGI say they won't launch your callbacks as a coroutine.
They do launch it as a greenlet (--greenlet
option) and they do handle asyncio
event loop (--asyncio 100
option), though.
That's enough for us to implement a replacement for the await
keyword that'd swap contexts with the main uWSGI event loop when you call this replacement.
Why would you need coroutines in a webserver anyways? You should respond to browser as soon as possible.
WebSockets
Given your uWSGI callable is wsgi_callback
, you can now utilize the whole power of the Python's asyncio
module inside of it:
def wsgi_callback(environ, start_response):
# You can await any futures/coroutines
result = await(my_coroutine())
# Since most of asyncio API are just coroutines, enjoy!
# Just a regular sleep
await(asyncio.sleep(2))
# Usage of the asyncio.wait
done, pending = await(asyncio.wait(
[coroutine1(), coroutine2()],
return_when=asyncio.FIRST_COMPLETE
))
# Don't forget to finally return your response someday
return []