asyncio
asyncio 自己是一個消息循環
步驟:
建立消息循環
把協程導入
關閉
1 import threading 2 # 引入異步io包 3 import asyncio 4 5 # 使用協程 6 @asyncio.coroutine 7 def hello(): 8 print("Hello Word! (%s)"% threading.currentThread()) 9 print("Start.....(%s)"% threading.currentThread()) 10 yield from asyncio.sleep(10) 11 print("Done....(%s)"% threading.currentThread()) 12 print("Hello again! (%s)" % threading.currentThread()) 13 14 # 啓動消息循環 15 loop = asyncio.get_event_loop() 16 # 定義任務 17 tasks = [hello(), hello()] 18 # asyncio使用wait等待task執行完畢 19 loop.run_until_complete(asyncio.wait(tasks)) 20 # 關閉消息循環 21 loop.close()
1 import asyncio 2 3 @asyncio.coroutine 4 def wget(host): 5 print("wget %s..." % host) 6 # 異步請求網絡地址 7 connect = asyncio.open_connection(host, 80) 8 # 注意yield from 的用法 9 reader, write = yield from connect 10 header = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host 11 write.write(header.encode('utf-8')) 12 yield from write.drain() 13 while True: 14 line = yield from reader.readline() 15 # http協議的換行使用\r\n 16 if line == b'\r\n': 17 break 18 print('%s header > %s' % (host, line.decode('utf-8').rstrip())) 19 20 write.close() 21 22 loop = asyncio.get_event_loop() 23 tasks = [wget(host) for host in ['www.baidu.com']] 24 loop.run_until_complete(asyncio.wait(tasks)) 25 loop.close()
async and await
爲了更好的的表示異步io
讓協程代碼更簡潔
使用上,能夠簡單的進行替換
用async替換¥ asyncio。coroutine
用await替換yield from
aiohttp
asyncio 實現單線程的併發io,在客戶端用處不大
在服務器端能夠asyncio+coroutine配合,由於http是io操做
asyncio實現了tcp, udp,ssl等協議
aiohttp是給予astncio實現的http框架
pip install aiohttp安裝
concurrent.futures
相似其餘語言的線程池的概念
利用multiprocessiong實現真正的並行計算
核心原理:以子進程的形式,並行運行多個python解釋器,從而令python程序能夠利用多核cpu來提高執行速度
1 from concurrent.futures import ThreadPoolExecutor 2 import time 3 4 def return_future(msg): 5 time.sleep(3) 6 return msg 7 8 # 建立一個線程池 9 pool = ThreadPoolExecutor(max_workers = 2) 10 11 # 向線程池加入兩個task 12 f1 = pool.submit(return_future, 'hello') 13 f2 = pool.submit(return_future, 'world') 14 # 等待執行完畢 15 print(f1.done()) 16 time.sleep(3) 17 print(f2.done()) 18 # 結果 19 print(f1.result()) 20 print(f2.result())
current 中map函數
跟map函數相似
函數須要異步執行
timeout:超時時間
map跟submit使用一個就行
1 import time, re 2 import os,datetime 3 4 from concurrent import futures 5 6 data = ['1', '2'] 7 8 def wait_on(argument): 9 print(argument) 10 time.sleep(2) 11 return 'ok' 12 13 ex = futures.ThreadPoolExecutor(max_workers = 2) 14 for i in ex.map(wait_on, data): 15 print(i)