[英]Python: How to wait until a function has been called in a different thread?
I have a function in thread A which needs to wait until a function in thread B is called.我在线程 A 中有一个 function 需要等到线程 B 中的 function 被调用。
The function in thread B is called periodically, so it just needs to wait until the next time it is called.线程B中的function是周期性调用的,所以只需要等到下一次调用。 This allows me to sync up with it.
这使我可以与之同步。
How would I do this?我该怎么做?
(Sorry if this is trivial.) (对不起,如果这是微不足道的。)
It may be a principle of computer science that no multithreading question is trivial.没有多线程问题是微不足道的,这可能是计算机科学的一条原则。
There are various ways to do this, but one of the simplest involves the use of a threading.Event object.有多种方法可以做到这一点,但最简单的方法之一是使用线程。事件 object。 Events are the simplest of the so-called synchronization primitives.
事件是所谓的同步原语中最简单的。 See the manual section on the threading module for more ideas.
有关更多想法,请参阅 threading 模块的手册部分。 Here is a working example:
这是一个工作示例:
#! python3.8
import threading
import time
t0 = time.time()
def elapsed_time():
return time.time() - t0
class StopMe:
def __init__(self):
self.running = True
def main():
ev1 = threading.Event()
stop = StopMe()
th1 = threading.Thread(target=thread1, args=(ev1, stop))
th1.start()
for _ in range(10):
ev1.wait()
print("The function was just called", elapsed_time())
ev1.clear()
stop.running = False
th1.join()
print("Exit", elapsed_time())
def thread1(event, stop):
def a_function():
event.set()
print("I am the function", elapsed_time())
while stop.running:
time.sleep(1.0)
a_function()
main()
Output: Output:
I am the function 1.0116908550262451
The function was just called 1.0116908550262451
I am the function 2.0219264030456543
The function was just called 2.0219264030456543
I am the function 3.0322916507720947
The function was just called 3.0322916507720947
I am the function 4.033170938491821
The function was just called 4.033170938491821
I am the function 5.043376445770264
The function was just called 5.043376445770264
I am the function 6.043909788131714
The function was just called 6.043909788131714
I am the function 7.054021596908569
The function was just called 7.054021596908569
I am the function 8.06399941444397
The function was just called 8.06399941444397
I am the function 9.064924716949463
The function was just called 9.064924716949463
I am the function 10.066757678985596
The function was just called 10.066757678985596
I am the function 11.076870918273926
Exit 11.076870918273926
Some things to note here:这里需要注意的一些事项:
Once you put a synchronization primitive into your code, you need to give some thought about how to terminate the thread gracefully, and how to terminate the application as a whole.将同步原语放入代码后,您需要考虑如何优雅地终止线程,以及如何整体终止应用程序。 In this example, the threads communicate through the little "StopMe" object, and through the Event object.
在此示例中,线程通过小“StopMe”object 和事件 object 进行通信。 Note that the main thread may have to wait one second until the secondary thread finishes its sleep function.
请注意,主线程可能需要等待一秒钟,直到辅助线程完成其睡眠 function。 That occurs if thread1 begins its time delay before the main thread calls the join function.
如果 thread1 在主线程调用连接 function 之前开始其时间延迟,则会发生这种情况。 That didn't happen in my test run but it might, depending on how CPU time slices are given to the different threads.
这在我的测试运行中没有发生,但它可能会发生,这取决于 CPU 时间片是如何分配给不同线程的。 If that's not acceptable to you, you have to write more code to get around it.
如果这对您来说是不可接受的,那么您必须编写更多代码来解决它。
Also note that the function call ev1.wait() will block the main thread until the event is set from the secondary thread.另请注意,function 调用 ev1.wait() 将阻塞主线程,直到从辅助线程设置事件。 In a GUI application that is not what you want.
在不是您想要的 GUI 应用程序中。
I ran this with Python3.8 but the program doesn't use any version-specific features, so it should work the same with any reasonably recent version of Python.我使用 Python3.8 运行它,但该程序不使用任何特定于版本的功能,因此它应该与任何合理的最新版本 Python 相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.