Python自定義進(jìn)程池實(shí)例分析【生產(chǎn)者、消費(fèi)者模型問題】
本文實(shí)例分析了Python自定義進(jìn)程池。分享給大家供大家參考,具體如下:
代碼說明一切:
#encoding=utf-8 #author: walker #date: 2014-05-21 #function: 自定義進(jìn)程池遍歷目錄下文件 from multiprocessing import Process, Queue, Lock import time, os #消費(fèi)者 class Consumer(Process): def __init__(self, queue, ioLock): super(Consumer, self).__init__() self.queue = queue self.ioLock = ioLock def run(self): while True: task = self.queue.get() #隊(duì)列中無任務(wù)時(shí),會(huì)阻塞進(jìn)程 if isinstance(task, str) and task == 'quit': break; time.sleep(1) #假定任務(wù)處理需要1秒鐘 self.ioLock.acquire() print( str(os.getpid()) + ' ' + task) self.ioLock.release() self.ioLock.acquire() print 'Bye-bye' self.ioLock.release() #生產(chǎn)者 def Producer(): queue = Queue() #這個(gè)隊(duì)列是進(jìn)程/線程安全的 ioLock = Lock() subNum = 4 #子進(jìn)程數(shù)量 workers = build_worker_pool(queue, ioLock, subNum) start_time = time.time() for parent, dirnames, filenames in os.walk(r'D:\test'): for filename in filenames: queue.put(filename) ioLock.acquire() print('qsize:' + str(queue.qsize())) ioLock.release() while queue.qsize() > subNum * 10: #控制隊(duì)列中任務(wù)數(shù)量 time.sleep(1) for worker in workers: queue.put('quit') for worker in workers: worker.join() ioLock.acquire() print('Done! Time taken: {}'.format(time.time() - start_time)) ioLock.release() #創(chuàng)建進(jìn)程池 def build_worker_pool(queue, ioLock, size): workers = [] for _ in range(size): worker = Consumer(queue, ioLock) worker.start() workers.append(worker) return workers if __name__ == '__main__': Producer()
ps:
self.ioLock.acquire() ... self.ioLock.release()
可用:
with self.ioLock: ...
替代。
再來一個(gè)好玩的例子:
#encoding=utf-8 #author: walker #date: 2016-01-06 #function: 一個(gè)多進(jìn)程的好玩例子 import os, sys, time from multiprocessing import Pool cur_dir_fullpath = os.path.dirname(os.path.abspath(__file__)) g_List = ['a'] #修改全局變量g_List def ModifyDict_1(): global g_List g_List.append('b') #修改全局變量g_List def ModifyDict_2(): global g_List g_List.append('c') #處理一個(gè) def ProcOne(num): print('ProcOne ' + str(num) + ', g_List:' + repr(g_List)) #處理所有 def ProcAll(): pool = Pool(processes = 4) for i in range(1, 20): #ProcOne(i) #pool.apply(ProcOne, (i,)) pool.apply_async(ProcOne, (i,)) pool.close() pool.join() ModifyDict_1() #修改全局變量g_List if __name__ == '__main__': ModifyDict_2() #修改全局變量g_List print('In main g_List :' + repr(g_List)) ProcAll()
Windows7 下運(yùn)行的結(jié)果:
λ python3 demo.py In main g_List :['a', 'b', 'c'] ProcOne 1, g_List:['a', 'b'] ProcOne 2, g_List:['a', 'b'] ProcOne 3, g_List:['a', 'b'] ProcOne 4, g_List:['a', 'b'] ProcOne 5, g_List:['a', 'b'] ProcOne 6, g_List:['a', 'b'] ProcOne 7, g_List:['a', 'b'] ProcOne 8, g_List:['a', 'b'] ProcOne 9, g_List:['a', 'b'] ProcOne 10, g_List:['a', 'b'] ProcOne 11, g_List:['a', 'b'] ProcOne 12, g_List:['a', 'b'] ProcOne 13, g_List:['a', 'b'] ProcOne 14, g_List:['a', 'b'] ProcOne 15, g_List:['a', 'b'] ProcOne 16, g_List:['a', 'b'] ProcOne 17, g_List:['a', 'b'] ProcOne 18, g_List:['a', 'b'] ProcOne 19, g_List:['a', 'b']
Ubuntu 14.04下運(yùn)行的結(jié)果:
In main g_List :['a', 'b', 'c'] ProcOne 1, g_List:['a', 'b', 'c'] ProcOne 2, g_List:['a', 'b', 'c'] ProcOne 3, g_List:['a', 'b', 'c'] ProcOne 5, g_List:['a', 'b', 'c'] ProcOne 4, g_List:['a', 'b', 'c'] ProcOne 8, g_List:['a', 'b', 'c'] ProcOne 9, g_List:['a', 'b', 'c'] ProcOne 7, g_List:['a', 'b', 'c'] ProcOne 11, g_List:['a', 'b', 'c'] ProcOne 6, g_List:['a', 'b', 'c'] ProcOne 12, g_List:['a', 'b', 'c'] ProcOne 13, g_List:['a', 'b', 'c'] ProcOne 10, g_List:['a', 'b', 'c'] ProcOne 14, g_List:['a', 'b', 'c'] ProcOne 15, g_List:['a', 'b', 'c'] ProcOne 16, g_List:['a', 'b', 'c'] ProcOne 17, g_List:['a', 'b', 'c'] ProcOne 18, g_List:['a', 'b', 'c'] ProcOne 19, g_List:['a', 'b', 'c']
可以看見Windows7下第二次修改沒有成功,而Ubuntu下修改成功了。據(jù)uliweb作者limodou講,原因是Windows下是充重啟實(shí)現(xiàn)的子進(jìn)程;Linux下是fork實(shí)現(xiàn)的。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python URL操作技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python實(shí)現(xiàn)類似ftp傳輸文件的網(wǎng)絡(luò)程序示例
這篇文章主要介紹了python實(shí)現(xiàn)類似ftp傳輸文件的網(wǎng)絡(luò)程序示例,需要的朋友可以參考下2014-04-04使用sklearn進(jìn)行對(duì)數(shù)據(jù)標(biāo)準(zhǔn)化、歸一化以及將數(shù)據(jù)還原的方法
今天小編就為大家分享一篇使用sklearn進(jìn)行對(duì)數(shù)據(jù)標(biāo)準(zhǔn)化、歸一化以及將數(shù)據(jù)還原的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07用Python實(shí)現(xiàn)一本個(gè)性化日歷
大家好,本篇文章主要講的是用Python實(shí)現(xiàn)一本個(gè)性化日歷,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02詳解Numpy中的數(shù)組拼接、合并操作(concatenate, append, stack, hstack, vstac
這篇文章主要介紹了詳解Numpy中的數(shù)組拼接、合并操作(concatenate, append, stack, hstack, vstack, r_, c_等),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05Python2 與Python3的版本區(qū)別實(shí)例分析
這篇文章主要介紹了Python2 與Python3的版本區(qū)別,結(jié)合實(shí)例形式分析了Python2 與Python3的版本使用過程中的各種常見區(qū)別、用法與注意事項(xiàng),需要的朋友可以參考下2020-03-03Python如何爬取實(shí)時(shí)變化的WebSocket數(shù)據(jù)的方法
這篇文章主要介紹了Python如何爬取實(shí)時(shí)變化的WebSocket數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03python bottle框架支持jquery ajax的RESTful風(fēng)格的PUT和DELETE方法
下面小編就為大家?guī)硪黄猵ython bottle框架支持jquery ajax的RESTful風(fēng)格的PUT和DELETE方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05