Python 3.4 introduced asyncio — asynchronous I/O and coroutines which are very powerful tools for writing concurrent code in a sequential fashion. As these tools become more powerful, we are seeing more libraries and frameworks that depend on
asyncio is a good thing; but, unfortunately many older frameworks such as Django and Flask don’t play nicely with coroutines.
Here’s a snippet that will run your coroutine synchronously:
1 2 3 4 5 6 7 8 9 10 11 12
Now in a function — defined by a framework that you are using and not marked as async – you can do this to run the coroutine sync:
1 2 3
This may not always be the best idea for long running tasks, but it can be useful in a pinch.