[英]How to find which have finished executing in Python
I am very new to the concept of threading and the concepts are still somewhat fuzzy. 我对线程的概念非常陌生,但这些概念仍然有些模糊。
But as of now i have a requirement in which i spin up an arbitrary number of threads from my Python program and then my Python program should indicate to the user running the process which threads have finished executing. 但是到目前为止,我有一个要求,即从我的Python程序中启动任意数量的线程,然后我的Python程序应向运行该进程的用户指示哪些线程已完成执行。 Below is my first try:
下面是我的第一次尝试:
import threading
from threading import Thread
from time import sleep
def exec_thread(n):
name = threading.current_thread().getName()
filename = name + ".txt"
with open(filename, "w+") as file:
file.write(f"My name is {name} and my main thread is {threading.main_thread()}\n")
sleep(n)
file.write(f"{name} exiting\n")
t1 = Thread(name="First", target=exec_thread, args=(10,))
t2 = Thread(name="Second", target=exec_thread, args=(2,))
t1.start()
t2.start()
while len(threading.enumerate()) > 1:
print(f"Waiting ... !")
sleep(5)
print(f"The threads are done"
So this basically tells me when all the threads are done executing. 因此,这基本上告诉我所有线程何时完成执行。
But i want to know as soon as any one of my threads have completed execution so that i can tell the user that please check the output file for the thread. 但是我想知道我的任何一个线程已经完成执行,以便我可以告诉用户请检查线程的输出文件。
I cannot use thread.join() since that would block my main program and the user would not know anything unless everything is complete which might take hours. 我不能使用thread.join(),因为那样会阻塞我的主程序,并且用户将一无所知,除非一切都完成了,这可能要花费数小时。 The user wants to know as soon as some results are available.
用户希望尽快获得一些结果。
Now i know that we can check individual threads whether they are active or not by doing : thread.isAlive()
but i was hoping for a more elegant solution in which if the child threads can somehow communicate with the main thread and say I am done ! 现在,我知道我们可以通过执行以下
thread.isAlive()
来检查各个线程是否处于活动状态: thread.isAlive()
但是我希望找到一个更优雅的解决方案,在该解决方案中,如果子线程可以以某种方式与主线程通信并说完成了!
Many thanks for any answers in advance. 非常感谢您提前提出任何答案。
The simplest and most straightforward way to indicate a single thread is "done" is to put the required notification in the thread's implementation method, as the very last step. 指示单个线程“完成”的最简单,最直接的方法是将所需的通知放入线程的实现方法中,这是最后一步。 For example, you could print out a notification to the user.
例如,您可以打印出一条通知给用户。
Or, you could use events, see: https://docs.python.org/3/library/threading.html#event-objects 或者,您可以使用事件,请参阅: https : //docs.python.org/3/library/threading.html#event-objects
This is one of the simplest mechanisms for communication between threads: one thread signals an event and other threads wait for it.
这是线程之间通信的最简单机制之一:一个线程发出事件信号,其他线程等待事件。
An event object manages an internal flag that can be set to true with the set() method and reset to false with the clear() method.
一个事件对象管理一个内部标志,该标志可以通过set()方法设置为true,而可以通过clear()方法设置为false。 The wait() method blocks until the flag is true.
wait()方法将阻塞,直到该标志为true。
So, the "final act" in your thread implementation would be to set an event object, and your main thread can wait until it's set. 因此,线程实现中的“最终操作”将是设置一个事件对象,而主线程可以等待直到它被设置。
Or, for an even fancier and more mechanism, use queues: https://docs.python.org/3/library/queue.html 或者,对于更高级的机制,请使用队列: https : //docs.python.org/3/library/queue.html
Each thread writes an "I'm done" object to the queue when done, and the main thread can read those notifications from the queue in sequence as each thread completes. 每个线程完成后都会向队列中写入一个“我已经完成”的对象,并且主线程可以在每个线程完成时依次从队列中读取那些通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.