asyncio
是Python 3.4版本引入的標准庫,直接內置了對異步IO的支持。
asyncio
的編程模型就是一個消息循環。我們從asyncio
模塊中直接獲取一個EventLoop
的引用,然后把需要執行的協程扔到EventLoop
中執行,就實現了異步IO。
用asyncio
實現Hello world
代碼如下:
import asyncio @asyncio.coroutine def hello(): print("Hello world!") # 異步調用asyncio.sleep(1): r = yield from asyncio.sleep(1) print("Hello again!") # 獲取EventLoop: loop = asyncio.get_event_loop() # 執行coroutine loop.run_until_complete(hello()) loop.close()
@asyncio.coroutine把一個generator標記為coroutine類型,然后,就把這個coroutine扔到eventloop中去執行
hello()會先打印出helloworld,然后yield from可以讓我們方便的調用另一個generator,由於asyncio.sleep(1)也是一個coroutine
所以線程不會等待asyncio.sleep而是直接中斷並執行下一個消息循環,當asyncio.sleep返回的時候,線程就在yield from拿到返回值,此處是None
然后執行下一個語句
把asyncio.sleep(1)看成是一個耗時1秒的IO操作。在此期間主線程沒有等待,而是去執行eventloop其他可以執行的coroutine因此可以實現並發執行
接下來封裝2個coroutine試試
import threading import asyncio @asyncio.coroutine def hello(): print('Hello world! (%s)' % threading.currentThread()) yield from asyncio.sleep(1) print('Hello again! (%s)' % threading.currentThread()) loop = asyncio.get_event_loop() tasks = [hello(), hello()] loop.run_until_complete(asyncio.wait(tasks)) loop.close()
Hello world! (<_MainThread(MainThread, started 140735195337472)>) Hello world! (<_MainThread(MainThread, started 140735195337472)>) (暫停約1秒) Hello again! (<_MainThread(MainThread, started 140735195337472)>) Hello again! (<_MainThread(MainThread, started 140735195337472)>)
由打印的當前線程名稱可以看出,兩個coroutine是由同一個線程並發執行的。
如果把asyncio.sleep()換成真正的IO操作,則多個coroutine就可以由一個線程並發執行。
我們用asyncio的異步網絡連接來獲取sina、sohu和163的網站首頁:
import asyncio @asyncio.coroutine def wget(host): print('wget %s...' % host) connect = asyncio.open_connection(host, 80) reader, writer = yield from connect header = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host writer.write(header.encode('utf-8')) yield from writer.drain() while True: line = yield from reader.readline() if line == b'\r\n': break print('%s header > %s' % (host, line.decode('utf-8').rstrip())) # Ignore the body, close the socket writer.close() loop = asyncio.get_event_loop() tasks = [wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']] loop.run_until_complete(asyncio.wait(tasks)) loop.close()
wget www.sohu.com... wget www.sina.com.cn... wget www.163.com... (等待一段時間) (打印出sohu的header) www.sohu.com header > HTTP/1.1 200 OK www.sohu.com header > Content-Type: text/html ... (打印出sina的header) www.sina.com.cn header > HTTP/1.1 200 OK www.sina.com.cn header > Date: Wed, 20 May 2015 04:56:33 GMT ... (打印出163的header) www.163.com header > HTTP/1.0 302 Moved Temporarily www.163.com header > Server: Cdn Cache Server V2.0
asyncio
提供了完善的異步IO支持;
異步操作需要在coroutine
中通過yield from
完成;
多個coroutine
可以封裝成一組Task然后並發執行。