日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python之路:线程池

發(fā)布時(shí)間:2024/4/15 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python之路:线程池 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

版本一

#!/usr/bin/env??python
#
?--*--coding:utf-8?--*--
import?Queue
import?threading


class?ThreadPool(object):
#創(chuàng)建類
????def?__init__(self,?max_num=20):#進(jìn)程函數(shù),默認(rèn)最大20個(gè)進(jìn)程
????????self.queue?=?Queue.Queue(max_num)#生成進(jìn)程
????????for?i?in?xrange(max_num):#循環(huán)進(jìn)程
????????????self.queue.put(threading.Thread)#上傳進(jìn)程

????def?get_thread(self):#下載進(jìn)程函數(shù)
????????return?self.queue.get()

????def?add_thread(self):#生成進(jìn)程函數(shù)
????????self.queue.put(threading.Thread)


pool?=?ThreadPool(10)#執(zhí)行類,并傳默認(rèn)進(jìn)程數(shù)值

def?func(arg,?p):#打印進(jìn)程
????print?arg
????import?time
????time.sleep(2)#間隔2秒
????p.add_thread()


for?i?in?xrange(30):#循環(huán)進(jìn)程
????thread?=?pool.get_thread()
????t?=?thread(target=func,?args=(i,?pool))#傳值到func函數(shù),并且執(zhí)行
????t.start()

?版本二

from?Queue?import?Queue
import?contextlib
import?threading
?
WorkerStop?=?object()
?
?
class?ThreadPool:
?
????workers?=?0
?
????threadFactory?=?threading.Thread
????currentThread?=?staticmethod(threading.currentThread)
?
????def?__init__(self,?maxthreads=20,?name=None):
?
????????self.q?=?Queue(0)
????????self.max?=?maxthreads
????????self.name?=?name
????????self.waiters?=?[]
????????self.working?=?[]
?
????def?start(self):
????????while?self.workers?<?min(self.max,?self.q.qsize()+len(self.working)):
????????????self.startAWorker()
?
????def?startAWorker(self):
????????self.workers?+
=?1
????????
name?=?"PoolThread-%s-%s"?%?(self.name?or?id(self),?self.workers)
????????newThread?
=?self.threadFactory(target=self._worker,?name=name)
????????
newThread.start()
?
????def?callInThread(self,?func,?*args,?**kw):
????????self.callInThreadWithCallback(None,?func,?*args,?**kw)
?
????def?callInThreadWithCallback(self,?onResult,?func,?*args,?**kw):
????????o?
=?(func,?args,?kw,?onResult)
????????self.q.put(o)
?
?
????@contextlib.contextmanager
????def?_workerState(self,?stateList,?workerThread):
????????stateList.append(workerThread)
????????try:
????????????yield
????????finally:
????????????stateList.remove(workerThread)
?
????def?_worker(self):
????????ct?
=?self.currentThread()
????????
o?=?self.q.get()
????????
while?o?is?not?WorkerStop:
????????????with?self._workerState(self.working,?ct):
????????????????function,?args,?kwargs,?onResult?
=?o
????????????????
del?o
????????????????try:
????????????????????result?
=?function(*args,?**kwargs)
????????????????????success?
=?True
????????????????
except:
????????????????????success?
=?False
????????????????????
if?onResult?is?None:
????????????????????????pass
?
????????????????????else:
????????????????????????pass
?
????????????????del?function,?args,?kwargs
?
????????????????if?onResult?is?not?None:
????????????????????try:
????????????????????????onResult(success,?result)
????????????????????except:
????????????????????????#context.call(ctx,?log.err)
????????????????????????pass
?
????????????????del?onResult,?result
?
????????????with?self._workerState(self.waiters,?ct):
????????????????o?
=?self.q.get()
?
????
def?stop(self):
????????while?self.workers:
????????????self.q.put(WorkerStop)
????????????self.workers?-
=?1
?
?
"""
def?show(arg):
????import?time
????time.sleep(1)
????print?arg
?
?
pool?
=?ThreadPool(20)
?
for?i?in?range(500):
????pool.callInThread(show,?i)
?
pool.start()
pool.stop()
"""

?

轉(zhuǎn)載于:https://www.cnblogs.com/wulaoer/p/5122472.html

總結(jié)

以上是生活随笔為你收集整理的Python之路:线程池的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。