我在某处读到KeyboardInterrupt
异常只在Python的主线程中引发。我还读到在子线程执行时主线程被阻塞。那么,这是否意味着CTRL+ C永远不会到达子线程。我尝试了以下代码:
def main():
try:
thread = threading.Thread(target=f)
thread.start() # thread is totally blocking (e.g., while True)
thread.join()
except KeyboardInterrupt:
print "Ctrl+C pressed..."
sys.exit(1)
def f():
while True:
pass # do the actual work
在这种情况下,CTRL+ C对执行没有影响。它就像是无法收听信号。我理解这是错误的方式吗?有没有其他方法可以使用CTRL+ 杀死线程C?
发布于 2019-06-21 12:42:45
你正在使用的问题是thread1.join()
,这将导致你的程序等到该线程完成继续。
信号将始终被主进程捕获,因为它是接收信号的信号,它是具有线程的进程。
如你所示,你基本上运行一个没有线程功能的“普通”应用程序,因为你启动1个线程并等到它完成继续。
https://stackoverflow.com/questions/-100009099
复制相似问题