在Python线程中,如何跟踪线程的完成情况? [英] In Python threading, how I can I track a thread's completion?

查看:409
本文介绍了在Python线程中,如何跟踪线程的完成情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成许多线程的python程序.这些线程的持续时间介于2秒到30秒之间.在主线程中,我想跟踪每个线程何时完成并打印一条消息.如果我只是按顺序.join()所有线程,而第一个线程持续30秒而其他线程完成得更快,我将无法更快地打印消息-所有消息将在30秒后打印.

I've a python program that spawns a number of threads. These threads last anywhere between 2 seconds to 30 seconds. In the main thread I want to track whenever each thread completes and print a message. If I just sequentially .join() all threads and the first thread lasts 30 seconds and others complete much sooner, I wouldn't be able to print a message sooner -- all messages will be printed after 30 seconds.

基本上我想阻止直到 any 线程完成.线程完成后,立即打印有关该消息的信息,如果还有其他线程仍然存在,请返回到阻塞状态.如果所有线程都已完成,则退出程序.

Basically I want to block until any thread completes. As soon as a thread completes, print a message about it and go back to blocking if any other threads are still alive. If all threads are done then exit program.

我想到的一种方法是让一个队列传递到所有线程并在queue.get()上阻塞.每当从队列接收到消息时,都将其打印出来,使用threading.active_count()检查是否还有其他线程处于活动状态,如果是,则返回对queue.get()的阻止.这将起作用,但是这里所有线程都需要遵循在终止之前将消息发送到队列的原则.

One way I could think of is to have a queue that is passed to all the threads and block on queue.get(). Whenever a message is received from the queue, print it, check if any other threads are alive using threading.active_count() and if so, go back to blocking on queue.get(). This would work but here all the threads need to follow the discipline of sending a message to the queue before terminating.

我想知道这是否是实现此行为的常规方法,或者还有其他/更好的方法吗?

I'm wonder if this is the conventional way of achieving this behavior or are there any other / better ways ?

推荐答案

这里是@detly答案的一种变体,它使您可以从主线程中指定消息,而不是从目标函数中打印消息.这将创建一个包装函数,该函数调用您的目标,然后在终止之前打印一条消息.您可以将其修改为在每个线程完成后执行任何类型的标准清除.

Here's a variation on @detly's answer that lets you specify the messages from your main thread, instead of printing them from your target functions. This creates a wrapper function which calls your target and then prints a message before terminating. You could modify this to perform any kind of standard cleanup after each thread completes.

#!/usr/bin/python

import threading
import time

def target1():
    time.sleep(0.1)
    print "target1 running"
    time.sleep(4)

def target2():
    time.sleep(0.1)
    print "target2 running"
    time.sleep(2)

def launch_thread_with_message(target, message, args=[], kwargs={}):
    def target_with_msg(*args, **kwargs):
        target(*args, **kwargs)
        print message
    thread = threading.Thread(target=target_with_msg, args=args, kwargs=kwargs)
    thread.start()
    return thread

if __name__ == '__main__':
    thread1 = launch_thread_with_message(target1, "finished target1")
    thread2 = launch_thread_with_message(target2, "finished target2")

    print "main: launched all threads"

    thread1.join()
    thread2.join()

    print "main: finished all threads"

这篇关于在Python线程中,如何跟踪线程的完成情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆