Python连载42-异步协程函数
一、 asyncio 1.python3.4开始引入标准库之中,内置对异步io的支持 2.asyncio本身是一个消息循环 3.步骤: (1)创建消息循环 (2)把协程导入 (3)关闭 4.举例: import threading #引入异步io包 import asyncio #使用协程 @asyncio.coroutine def hello(): print("Hello World!(%s)"%threading.current_thread()) print("Start......(%s)"%threading.current_thread()) yield from asyncio.sleep(5) print("Done.....(%s)"%threading.current_thread()) print("Hello again!(%s)"%threading.current_thread()) #启动消息循环 loop = asyncio.get_event_loop() #定义任务 tasks = [hello(),hello()] #asyncio使用wait等待task执行完毕 loop.run_until_complete(asyncio.wait(tasks)) #关闭消息循环 loop.close() 二、asyncio and await 1