如何在读取流时正确终止 Python3 线程
我正在使用线程从流(/dev/tty1)读取字符串,同时处理主循环中的其他内容。我希望在按 CTRL-C 时线程与主程序一起终止。
from threading import Thread
class myReader(Thread):
def run(self):
with open('/dev/tty1', encoding='ascii') as myStream:
for myString in myStream:
print(myString)
def quit(self):
pass # stop reading, close stream, terminate the thread
myReader = Reader()
myReader.start()
while(True):
try:
pass # do lots of stuff
KeyboardInterrupt:
myReader.quit()
raise
通常的解决方案 - run() 循环内的布尔变量 - 在这里不起作用。处理这个问题的推荐方法是什么?
我可以只设置守护进程标志,但随后我将无法使用 quit() 方法,该方法稍后可能会被证明是有价值的(进行一些清理)。有什么想法吗?
I'm using a thread to read Strings from a stream (/dev/tty1) while processing other things in the main loop. I would like the Thread to terminate together with the main program when pressing CTRL-C.
from threading import Thread
class myReader(Thread):
def run(self):
with open('/dev/tty1', encoding='ascii') as myStream:
for myString in myStream:
print(myString)
def quit(self):
pass # stop reading, close stream, terminate the thread
myReader = Reader()
myReader.start()
while(True):
try:
pass # do lots of stuff
KeyboardInterrupt:
myReader.quit()
raise
The usual solution - a boolean variable inside the run() loop - doesn't work here. What's the recommended way to deal with this?
I can just set the Daemon flag, but then I won't be able to use a quit() method which might prove valuable later (to do some clean-up). Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AFAIK,Python 3 中没有内置机制(就像 Python 2 一样)。您是否尝试过使用
PyThreadState_SetAsyncExc
进行验证的 Python 2 方法,记录在此处 和 此处,或替代跟踪方法这里?这是上面的
PyThreadState_SetAsyncExc
方法的稍微修改版本:AFAIK, there is no built-in mechanism for that in Python 3 (just as in Python 2). Have you tried the proven Python 2 approach with
PyThreadState_SetAsyncExc
, documented here and here, or the alternative tracing approach here?Here's a slightly modified version of the
PyThreadState_SetAsyncExc
approach from above:让您的线程成为守护线程。当所有非守护线程都退出时,程序退出。因此,当 Ctrl-C 传递给程序并且主线程退出时,无需显式终止读取器。
Make your thread a daemon thread. When all non-daemon threads have exited, the program exits. So when Ctrl-C is passed to your program and the main thread exits, there's no need to explicitly kill the reader.