python實(shí)現(xiàn)線程池的方法
本文實(shí)例講述了python實(shí)現(xiàn)線程池的方法。分享給大家供大家參考。具體如下:
原理:建立一個(gè)任務(wù)隊(duì)列,然多個(gè)線程都從這個(gè)任務(wù)隊(duì)列中取出任務(wù)然后執(zhí)行,當(dāng)然任務(wù)隊(duì)列要加鎖,詳細(xì)請(qǐng)看代碼
文件名:thrd_pool.py 系統(tǒng)環(huán)境:ubuntu linux & python2.6
import threading
import time
import signal
import os
class task_info(object):
def __init__(self):
self.func = None
self.parm0 = None
self.parm1 = None
self.parm2 = None
class task_list(object):
def __init__(self):
self.tl = []
self.mutex = threading.Lock()
self.sem = threading.Semaphore(0)
def append(self, ti):
self.mutex.acquire()
self.tl.append(ti)
self.mutex.release()
self.sem.release()
def fetch(self):
self.sem.acquire()
self.mutex.acquire()
ti = self.tl.pop(0)
self.mutex.release()
return ti
class thrd(threading.Thread):
def __init__(self, tl):
threading.Thread.__init__(self)
self.tl = tl
def run(self):
while True:
tsk = self.tl.fetch()
tsk.func(tsk.parm0, tsk.parm1, tsk.parm2)
class thrd_pool(object):
def __init__(self, thd_count, tl):
self.thds = []
for i in range(thd_count):
self.thds.append(thrd(tl))
def run(self):
for thd in self.thds:
thd.start()
def func(parm0=None, parm1=None, parm2=None):
print 'count:%s, thrd_name:%s'%(str(parm0), threading.currentThread().getName())
def cleanup(signo, stkframe):
print ('Oops! Got signal %s', signo)
os._exit(0)
if __name__ == '__main__':
signal.signal(signal.SIGINT, cleanup)
signal.signal(signal.SIGQUIT, cleanup)
signal.signal(signal.SIGTERM, cleanup)
tl = task_list()
tp = thrd_pool(6, tl)
tp.run()
count = 0
while True:
ti = task_info()
ti.parm0 = count
ti.func = func
tl.append(ti)
count += 1
time.sleep(2)
pass
執(zhí)行方式:python thrd_pool.py
執(zhí)行結(jié)果:
count:0, thrd_name:Thread-1
count:1, thrd_name:Thread-2
count:2, thrd_name:Thread-3
count:3, thrd_name:Thread-4
count:4, thrd_name:Thread-5
count:5, thrd_name:Thread-1
count:6, thrd_name:Thread-6
count:7, thrd_name:Thread-2
count:8, thrd_name:Thread-3
count:9, thrd_name:Thread-4
count:10, thrd_name:Thread-5
count:11, thrd_name:Thread-1
count:12, thrd_name:Thread-6
count:13, thrd_name:Thread-2
count:14, thrd_name:Thread-3
('Oops! Got signal %s', 15)
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
對(duì)Python中 \r, \n, \r\n的徹底理解
這篇文章主要介紹了對(duì)Python中 \r, \n, \r\n的徹底理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
如何使用python對(duì)圖片進(jìn)行批量壓縮詳解
對(duì)于圖片強(qiáng)行被定義高和寬會(huì)變形,我們希望圖片被改變大小后,比例保持不變,完成對(duì)圖片的壓縮,下面這篇文章主要給大家介紹了關(guān)于如何使用python對(duì)圖片進(jìn)行批量壓縮的相關(guān)資料,需要的朋友可以參考下2022-07-07
Python?操作?MongoDB數(shù)據(jù)庫(kù)的方法(非?ODM)
這篇文章主要介紹了Python?操作?MongoDB?----非?ODM的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
Python 查找list中的某個(gè)元素的所有的下標(biāo)方法
今天小編就為大家分享一篇Python 查找list中的某個(gè)元素的所有的下標(biāo)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Django返回HTML文件的實(shí)現(xiàn)方法
這篇文章主要介紹了Django返回HTML文件的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Python內(nèi)置函數(shù)reversed()用法分析
這篇文章主要介紹了Python內(nèi)置函數(shù)reversed()用法,結(jié)合實(shí)例形式分析了reversed()函數(shù)的功能及針對(duì)序列元素相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2018-03-03

