检查一个concurrent.futures.ThreadPoolExecutor [英] Checking up on a `concurrent.futures.ThreadPoolExecutor`
问题描述
我有一个实时的 concurrent.futures.ThreadPoolExecutor
。我要检查其状态。我想知道有多少个线程,有多少个正在处理任务,哪些任务,有多少空闲,以及队列中有哪些任务。我怎么能找到这些东西?
I've got a live concurrent.futures.ThreadPoolExecutor
. I want to check its status. I want to know how many threads there are, how many are handling tasks and which tasks, how many are free, and which tasks are in the queue. How can I find out these things?
推荐答案
对Pool和挂起的工作项队列有一些可见性。要找出可用的内容,请打印 poolx .__ dict __
来查看结构。阅读ThreadPool代码,它非常好:并发。 futures.thread
There is some visibility into the Pool, and the pending workitem queue. To find out what's available, print poolx.__dict__
to see the structure. Read the ThreadPool code, it's pretty good: concurrent.futures.thread
以下内容创建了一个线程池。然后,它创建了两个作业:一个睡眠3秒,另一个立即返回。然后打印该池中待处理的工作项数。
The following creates a pool with one thread. It then creates two jobs: one sleeps for 3 seconds, the other immediately returns. The pool's number of pending work items is then printed.
然后,我们从工作队列中打印出工作项。在这种情况下,线程已经在执行 time.sleep(3)
函数,因此不在队列中。函数 sleep
,带有args [0]
和kwargs {}
,因为这是要运行的池的下一个工作项。
Following that, we print out items from the work queue. In this case, a thread is already executing the time.sleep(3)
function, so that's not in the queue. The function sleep
with args [0]
and kwargs {}
is printed, because that's the next work item for the pool to run.
对于无破坏性队列见解,@ dano和@abarnert表示敬意。
Kudos to @dano for the nondestructive queue insight, and @abarnert.
import concurrent.futures, time
poolx = concurrent.futures.ThreadPoolExecutor(max_workers=1)
poolx.submit(time.sleep, 3)
poolx.submit(time.sleep, 0) # very fast
print('pending:', poolx._work_queue.qsize(), 'jobs')
print('threads:', len(poolx._threads))
print()
# TODO: make thread safe; work on copy of queue?
print('Estimated Pending Work Queue:')
for num,item in enumerate(poolx._work_queue.queue):
print('{}\t{}\t{}\t{}'.format(
num+1, item.fn, item.args, item.kwargs,
))
poolx.shutdown(wait=False)
输出
output
pending: 1 jobs
threads: 1
Pending Work Queue:
1 <built-in function sleep> (0,) {}
这篇关于检查一个concurrent.futures.ThreadPoolExecutor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!