asyncio
是Python 3.4版本引入的標準庫,直接內置了對異步IO的支持。html
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拿到返回值,此處是Nonesocket
而後執行下一個語句async
把asyncio.sleep(1)當作是一個耗時1秒的IO操做。在此期間主線程沒有等待,而是去執行eventloop其餘能夠執行的coroutine所以能夠實現併發執行oop
接下來封裝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支持;spa
異步操做須要在coroutine
中經過yield from
完成;
多個coroutine
能夠封裝成一組Task而後併發執行。