python并发之concurrent.futures
concurrent:并發(fā)
Python標(biāo)準(zhǔn)庫(kù)為我們提供了threading和multiprocessing模塊編寫(xiě)相應(yīng)的多線程/多進(jìn)程代碼。從Python3.2開(kāi)始,標(biāo)準(zhǔn)庫(kù)為我們提供了concurrent.futures模塊,它提供了ThreadPoolExecutor和ProcessPoolExecutor兩個(gè)類,實(shí)現(xiàn)了對(duì)threading和multiprocessing的更高級(jí)的抽象,對(duì)編寫(xiě)線程池/進(jìn)程池提供了直接的支持。?
concurrent.futures基礎(chǔ)模塊是executor和future。
Executor
Executor是一個(gè)抽象類,它不能被直接使用。它為具體的異步執(zhí)行定義了一些基本的方法。?ThreadPoolExecutor和ProcessPoolExecutor繼承了Executor,分別被用來(lái)創(chuàng)建線程池和進(jìn)程池的代碼。
submit方法
Executor中定義了submit()方法,這個(gè)方法的作用是提交一個(gè)可執(zhí)行的回調(diào)task,并返回一個(gè)future實(shí)例。future對(duì)象代表的就是給定的調(diào)用。
我們使用submit方法來(lái)往線程池中加入一個(gè)task,submit返回一個(gè)Future對(duì)象,對(duì)于Future對(duì)象可以簡(jiǎn)單地理解為一個(gè)在未來(lái)完成的操作。
map方法
Exectuor還為我們提供了map方法,和內(nèi)建的map用法類似。映射。
future
Future實(shí)例是由Executor.submit()創(chuàng)建的。可以理解為一個(gè)在未來(lái)完成的操作,這是異步編程的基礎(chǔ)。通常情況下,我們執(zhí)行io操作,訪問(wèn)url時(shí)(如下)在等待結(jié)果返回之前會(huì)產(chǎn)生阻塞,cpu不能做其他事情,而Future的引入幫助我們?cè)诘却倪@段時(shí)間可以完成其他的操作。
示例:
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor import os,time,random def foo(i):print('%s is running %s'%(os.getpid(),i))time.sleep(random.randint(1, 3))return i**2 if __name__ == '__main__':print('cpu_num:',os.cpu_count())executor=ProcessPoolExecutor()print('executor',executor,type(executor))# futures=[]# for i in range(10):# future=executor.submit(foo,i)# futures.append(future)futures=[executor.submit(foo,i) for i in range(10)]executor.shutdown()#程序運(yùn)行到這里有明顯的時(shí)間間隔,可見(jiàn)是在shutdown存在的情況下,程序?qū)uture全部執(zhí)行完,才繼續(xù)往下走的print('主')print(futures)for future in futures:print(future.result())
輸出:
cpu_num: 8 executor <concurrent.futures.process.ProcessPoolExecutor object at 0x00000276745AA978> <class 'concurrent.futures.process.ProcessPoolExecutor'> 11740 is running 0 3156 is running 1 9928 is running 2 2208 is running 3 2324 is running 4 13080 is running 5 1892 is running 6 2964 is running 7 2208 is running 8 2324 is running 9 主 [<Future at 0x27674900e10 state=finished returned int>, <Future at 0x27674949dd8 state=finished returned int>, <Future at 0x27674949e80 state=finished returned int>, <Future at 0x27674949f28 state=finished returned int>, <Future at 0x27674949fd0 state=finished returned int>, <Future at 0x2767495a0b8 state=finished returned int>, <Future at 0x2767495a198 state=finished returned int>, <Future at 0x2767495a278 state=finished returned int>, <Future at 0x2767495a358 state=finished returned int>, <Future at 0x2767495a438 state=finished returned int>] 0 1 4 9 16 25 36 49 64 81
利用ThreadProcessExecutor爬蟲(chóng)
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor import requests def get(url):r=requests.get(url)return {'url':url,'text':r.text} def parse(future):dic=future.result() #future對(duì)象調(diào)用result方法取其值、f=open('db.text','a')date='url:%s\n'%len(dic['text'])f.write(date)f.close() if __name__ == '__main__':executor=ThreadPoolExecutor()url_l = ['http://cn.bing.com/', 'http://www.cnblogs.com/wupeiqi/', 'http://www.cnblogs.com/654321cc/','https://www.cnblogs.com/', 'http://society.people.com.cn/n1/2017/1012/c1008-29581930.html','http://www.xilu.com/news/shaonianxinzangyou5gedong.html', ]futures=[]for url in url_l:executor.submit(get,url).add_done_callback(parse) #與Pool進(jìn)程池回調(diào)函數(shù)接收的是A函數(shù)的返回值(對(duì)象ApplyResult.get()得到的值)。executor.shutdown() #這里回調(diào)函數(shù)parse,接收的參數(shù)是submit生成的 Future對(duì)象。print('主')
輸出:
轉(zhuǎn)載于:https://www.cnblogs.com/654321cc/p/7677394.html
總結(jié)
以上是生活随笔為你收集整理的python并发之concurrent.futures的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mingW与cygwin
- 下一篇: Python 的字符串内建函数