Python中线程之间的通信(不使用全局变量)(Communication between threads in Python (without using Global Variables))
假设我们有一个主线程为测试模块启动两个线程 - “test_a”和“test_b”。 两个测试模块线程都保持其状态,无论它们是否已完成执行测试,或者是否遇到任何错误,警告或是否要更新其他一些信息。
主线程如何访问此信息并采取相应措施。 例如,如果“test_a”引发错误标志; “main”如何知道并在出现错误之前停止其余的测试?
一种方法是使用全局变量,但这很难看。很快。
Let's say if we have a main thread which launches two threads for test modules - " test_a" and " test_b". Both the test module threads maintain their state whether they are done performing test or if they encountered any error, warning or if they want to update some other information.
How main thread can get access to this information and act accordingly. For example, if " test_a" raised an error flag; How "main" will know and stop rest of the tests before existing with error ?
One way to do this is using global variables but that gets very ugly.. Very soon.
原文:https://stackoverflow.com/questions/16677370