Python 线程 - 当主线程引发异常时处理线程
我有一个从数据采集设备读取数据的线程。当我的主函数崩溃时,它不会杀死数据采集线程。如果线程外出现异常,如何停止数据采集?
我的程序的简化版本是:
class Daq_reader():
def __init__(self):
self.keepReading = True
self.data = []
def acquire(self):
while self.keepReading:
self.data = append_new_data(data)
time.sleep(1)
def main():
reader = Daq_reader()
daq_thread = threading.Thread(target=reader.acquire, daemon=True)
daq_thread.start()
# rest of program here that might crash
我尝试使用守护进程为 True 和 False。谢谢你!
I have a thread that reads data from a data acquisition device. When my main function crashes, it doesn't kill the data acquisition thread. How can I stop the data acquisition if there's an exception outside the thread?
A simplified version of my program is:
class Daq_reader():
def __init__(self):
self.keepReading = True
self.data = []
def acquire(self):
while self.keepReading:
self.data = append_new_data(data)
time.sleep(1)
def main():
reader = Daq_reader()
daq_thread = threading.Thread(target=reader.acquire, daemon=True)
daq_thread.start()
# rest of program here that might crash
I tried with daemon being True and False. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来你想杀死你的线程。您可以使用
python-worker
(链接)来创建和中止该工人。当您使用
@worker
装饰时,您的reader.acquire()
将自动在后台运行it looks like you want to kill your thread. You can use
python-worker
(link) to create and abort the worker.your
reader.acquire()
will be run in your background automatically when you decorate it with@worker