异步发展过程
1.用greenlet
from greenlet import greenlet def fun1(): print('1') def fun2(): print('2') g1=greenlet(fun1) g2=greenlet(fun2) g1.switch()
2.用yield
3.用asyncio
import asyncio @asyncio.coroutine def fun3(): yield from asyncio.sleep(2) print('3') tasks=[ asyncio.ensure_future(fun3()) ] asyncio.get_event_loop().run_until_complete(asyncio.wait(tasks)) 第三阶段也就是上面的第三条最后一步,随着python不断更新对第三阶段的的最后一步实现了简写
async def main(): tasks = [ asyncio.ensure_future(fun4()) ] done,pending= await asyncio.wait(tasks,timeout=None) print(done) # 此方法 在run方法中实get_event_loop().run_until_complete(asyncio.wait(tasks)) asyncio.run(main())