Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对于多个线程,一个线程通信,其他线程应该停止_Python_Multithreading_Global Variables - Fatal编程技术网

Python 对于多个线程,一个线程通信,其他线程应该停止

Python 对于多个线程,一个线程通信,其他线程应该停止,python,multithreading,global-variables,Python,Multithreading,Global Variables,还有比这更好的方法吗? 当前使用两个线程通过run\u break\u check方法查看的类变量根据break\u条件的结果更改值 class MyClass: def __init__(self): self._need_break = None def f(self): self._need_break = False def run_loop(): while not self._need_bre

还有比这更好的方法吗? 当前使用两个线程通过run\u break\u check方法查看的类变量根据break\u条件的结果更改值

class MyClass:
    def __init__(self):
        self._need_break = None

    def f(self):
        self._need_break = False

        def run_loop():
            while not self._need_break:
                pass # Some code

        def run_break_check():
            while not self._need_break:
                self._need_break = self.break_condition()

        threads = [
                     threading.Thread(target=run_loop),
                     threading.Thread(target=run_break_check())
                  ]
        for t in threads:
           t.start()
        for t in threads:
           t.join()

    def break_condition():
        # Some code
        return #Bool Return