1. 程式人生 > >異步調用與回調機制

異步調用與回調機制

clas print __name__ .text quest lee 提交 alex content

提交任務的兩種方式。

同步調用:提交完任務後,就在原地等待任務執行完畢,拿到結果,再執行下一行代碼,導致程序是串行執行

異步調用:提交完任務後,不等待任務執行完畢

from concurrent.futures import ThreadPoolExecutor
import time,random

def la(name):
    print(%s is laing%name)
    time.sleep(random.randint(3,5))
    res = random.randint(7,13)*#
    return {name:name,res
:res} def weigh(shit): shit = shit.result() # 異步回掉時,處理接收到的對象 name = shit[name] size = len(shit[res]) print(%s 拉了 《%s》kg%(name,size)) if __name__ ==__main__: pool = ThreadPoolExecutor(13) # 同步調用 # shit1 = pool.submit(la,‘alex‘).result() # weigh(shit1) #
shit2 = pool.submit(la, ‘huhao‘).result() # weigh(shit2) # shit3 = pool.submit(la, ‘zhanbin‘).result() # weigh(shit3) # 異步調用 pool.submit(la, alex).add_done_callback(weigh) pool.submit(la, huhao).add_done_callback(weigh) pool.submit(la, zhanbin).add_done_callback(weigh)

簡單網頁爬蟲示例:

import requests,time
from concurrent.futures import ThreadPoolExecutor

def get(url):
    print(get url,url)
    response = requests.get(url)
    time.sleep(3)
    return {url:url,content:response.text}

def parse(res):
    res = res.result()
    print(%s parse res is %s%(res[url],len(res[content])))

if __name__ == __main__:
    urls = [
        http://www.cnblogs.com/stin,
        https://www.python.org,
        https://www.openstack.org,
    ]
    pool = ThreadPoolExecutor(2)
    for url in urls:
        pool.submit(get,url).add_done_callback(parse)

異步調用與回調機制