异步corountine

2017-10-25  本文已影响0人  zzjack

示例

结合一下nodejs的async和golang的goroutine,python的真的很简单。

import threading
import asyncio
import time

async def hello():
    print('Hello world! (%s)' % threading.currentThread())
    await test()
    print('Hello again! (%s)' % threading.currentThread())

async def test():
    time.sleep(2)

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    tasks = [hello()]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()
上一篇下一篇

猜你喜欢

热点阅读