我们的自动化部署平台升级到django2+python3了,
并发方面的知识点,也跟进一下啦。
http://python.jobbole.com/87310/
这篇讲得挺入门的~~
实操一下。
import time
import asyncio
import functools
now = lambda : time.time()
async def do_some_work(x):
print('Waiting: ', x)
await asyncio.sleep(x)
return 'Done after {}s'.format(x)
def callback(t, future):
print('Callback: ', t, future.result())
start = now()
coroutine1 = do_some_work(1)
coroutine2 = do_some_work(2)
coroutine4 = do_some_work(4)
tasks = [
asyncio.ensure_future(coroutine1),
asyncio.ensure_future(coroutine2),
asyncio.ensure_future(coroutine4),
]
loop =asyncio.get_event_loop()
for task in tasks:
task.add_done_callback(functools.partial(callback, 2))
loop.run_until_complete(asyncio.wait(tasks))
for task in tasks:
task.add_done_callback(functools.partial(callback, 2))
print('Task return: {}'.format(task.result()))
print('TIME: ', now() - start)