python 线程池_python线程池threadpool使用方法

class WorkerThread(threading.Thread):

"""Background thread connected to the requests/results queues.

A worker thread sits in the background and picks up work requests from

one queue and puts the results in another until it is dismissed.

"""

def __init__(self, requests_queue, results_queue, poll_timeout=5, **kwds):

"""Set up thread in daemonic mode and start it immediatedly.

``requests_queue`` and ``results_queue`` are instances of

``Queue.Queue`` passed by the ``ThreadPool`` class when it creates a

new worker thread.

"""

threading.Thread.__init__(self, **kwds)

self.setDaemon(1)#

self._requests_queue = requests_queue#任务队列

self._results_queue = results_queue#任务结果队列

self._poll_timeout = poll_timeout#run函数中从任务队列中get任务时的超时时间,如果超时则继续while(true);

self._dismissed = threading.Event()#线程事件,如果set线程事件则run会执行break,直接退出工作线程;

self.start()

def run(self):

"""Repeatedly process the job queue until told to exit."""

while True:

if self._dismissed.isSet():#如果设置了self._dismissed则退出工作线程

# we are dismissed, break out of loop

break

# get next work request. If we don't get a new request from the

# queue after self._poll_timout seconds, we jump to the start of

# the while loop again, to give the thread a chance to exit.

try:

request = self._requests_queue.get(True, self._poll_timeout)

except Queue.Empty:#尝从任务 队列self._requests_queue 中get任务,如果队列为空,则continue

continue

else:

if self._dismissed.isSet():#检测此工作线程事件是否被set,如果被设置,意味着要结束此工作线程,那么就需要将取到的任务返回到任务队列中,并且退出线程

# we are dismissed, put back request in queue and exit loop

self._requests_queue.put(request)

break

try:

"""

self._dismissed.set()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python使用线程池有两种方式。一种是使用第三方库threadpool,另一种是使用Python3中新引入的库concurrent.futures.ThreadPoolExecutor。\[1\] 使用threadpool方法如下: 1. 导入threadpool库:`import threadpool` 2. 创建线程池:`pool = threadpool.ThreadPool(num_threads)` 其中,num_threads是线程池中线程的数量。 3. 创建任务:`request = threadpool.makeRequests(func, args)` 其中,func是要执行的函数,args是函数的参数。 4. 将任务添加到线程池中:`\[pool.putRequest(req) for req in request\]` 5. 等待所有任务完成:`pool.wait()` 使用concurrent.futures.ThreadPoolExecutor的方法如下: 1. 导入concurrent.futures库:`from concurrent.futures import ThreadPoolExecutor` 2. 创建线程池:`pool = ThreadPoolExecutor(max_workers=num_threads)` 其中,num_threads是线程池中线程的数量。 3. 提交任务:`future = pool.submit(func, *args)` 其中,func是要执行的函数,args是函数的参数。 4. 获取任务的结果:`result = future.result()` 这些方法可以让我们更加高效地利用CPU资源,提高程序的运行速度。\[2\]\[3\] #### 引用[.reference_title] - *1* [python 线程池使用](https://blog.csdn.net/weixin_45459224/article/details/126182031)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Python中多线程和线程池使用方法](https://blog.csdn.net/weixin_47831992/article/details/130170103)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Python线程池(thread pool)创建及使用+实例代码](https://blog.csdn.net/master_hunter/article/details/125070310)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值