Python 检查线程/从列表中删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4067786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking on a thread / remove from list
提问by Wizzard
I have a thread which extends Thread. The code looks a little like this;
我有一个扩展线程的线程。代码看起来有点像这样;
class MyThread(Thread):
def run(self):
# Do stuff
my_threads = []
while has_jobs() and len(my_threads) < 5:
new_thread = MyThread(next_job_details())
new_thread.run()
my_threads.append(new_thread)
for my_thread in my_threads
my_thread.join()
# Do stuff
So here in my pseudo code I check to see if there is any jobs (like a db etc) and if there is some jobs, and if there is less than 5 threads running, create new threads.
因此,在我的伪代码中,我检查是否有任何作业(例如 db 等)以及是否有一些作业,如果运行的线程少于 5 个,则创建新线程。
So from here, I then check over my threads and this is where I get stuck, I can use .join() but my understanding is that - this then waits until it's finished so if the first thread it checks is still in progress, it then waits till it's done - even if the other threads are finished....
所以从这里开始,我然后检查我的线程,这就是我卡住的地方,我可以使用 .join() 但我的理解是 - 然后等待它完成,所以如果它检查的第一个线程仍在进行中,它然后等到它完成 - 即使其他线程完成......
so is there a way to check if a thread is done, then remove it if so?
那么有没有办法检查线程是否完成,然后将其删除?
eg
例如
for my_thread in my_threads:
if my_thread.done():
# process results
del (my_threads[my_thread]) ?? will that work...
采纳答案by Arlaharen
As TokenMacGuy says, you should use thread.isAlive()to check if a thread is still running. To remove no longer running threads from your list you can use a list comprehension:
正如 TokenMacGuy 所说,您应该使用thread.isAlive()来检查线程是否仍在运行。要从列表中删除不再运行的线程,您可以使用列表理解:
for t in my_threads:
if not t.isAlive():
# get results from thtead
t.handled = True
my_threads = [t for t in my_threads if not t.handled]
This avoids the problem of removing items from a list while iterating over it.
这避免了在迭代列表时从列表中删除项目的问题。
回答by SingleNegationElimination
you need to call thread.isAlive()to find out if the thread is still running
您需要调用thread.isAlive()以查看线程是否仍在运行
回答by seriyPS
Better way is to use Queue class: http://docs.python.org/library/queue.html
更好的方法是使用 Queue 类:http: //docs.python.org/library/queue.html
Look at the good example code in the bottom of documentation page:
查看文档页面底部的优秀示例代码:
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.daemon = True
t.start()
for item in source():
q.put(item)
q.join() # block until all tasks are done
回答by Dre
The answer has been covered, but for simplicity...
答案已经涵盖,但为简单起见......
# To filter out finished threads
threads = [t for t in threads if t.is_alive()]
# Same thing but for QThreads (if you are using PyQt)
threads = [t for t in threads if t.isRunning()]
回答by William DeCook
mythreads = threading.enumerate()
Enumerate returns a list of all Thread objects still alive. https://docs.python.org/3.6/library/threading.html
Enumerate 返回所有仍然存活的 Thread 对象的列表。 https://docs.python.org/3.6/library/threading.html

