A lot of fuss has been around asyncio and tools for asynchronous programming integrated to python standard library starting with python 3.4 and without much of a surprise, a lot of talks where around this topic.
Although it would be hard to summarize all the talks, here are a few notes I took.
Random asyncio patterns
Executing some synchronous code in a task/future wrapped thread executor...
import asyncio
import time
def long_task():
print('starting ...')
time.sleep(2)
print('done!')
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(loop.run_in_executor(None, long_task))
finally:
loop.close()
Waiting on a list of futures and acting as soon as one is completed...
import asyncio
async def my_sleeping_task(name, secs):
print('Starting {}'.format(name))
await asyncio.sleep(secs)
print('Finished {}'.format(name))
return name
async def main():
tasks = [
my_sleeping_task('one', 3),
my_sleeping_task('two', 2),
my_sleeping_task('three', 1),
]
while len(tasks):
done, tasks = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
for task in done:
print(task, task.result())
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.close()
Note
asyncio.gather(...) can be used to get the results of a futures/coroutines collection, but you won't get the results as soon as the first future or coroutine is completed.
Twisted's still alive
Yes, Twisted is overlapping features with asyncio but in fact, it's a good news for them. They will be able to remove the async part from the library to focus on things asyncio is not meant to do.
Still, Twisted is a huge library with high stability requirements so it's not likely to happen before at least 2020.
New tools
Replace asyncio event loop uvloop, based on libuv:
- Benchmarks: http://magic.io/blog/uvloop-blazing-fast-python-networking/
- Sources https://github.com/MagicStack/uvloop
Talk with your database asynchronously: