从另一个线程引发异常以终止另一个线程

时间:2019-04-02 03:00:07

标签: python-3.x multithreading

我具有非循环功能,因此很难使用事件终止线程。是否可以从其他线程终止其他线程?

    def check_event(self, event):
        while True:
            if event.is_set():
                # Code to stop other thread
            time.sleep(1)

    def run(self, event):
        try:
            self.thread = threading.Thread(target=self.check_event, args=(event,))
            self.thread.start()

            ...
        except Exception:
            print('Stopped')

run方法在另一个线程中。

我的目标是在线程中创建一个循环,以查看是否将设置事件。如果将其设置,我将停止处理run方法的线程。

我尝试从check_event方法引发异常,但这给了我一个错误。

0 个答案:

没有答案
相关问题