Rap*_*hDG 7 python windows signals
我有我的主应用程序线程产生2个线程,我在主线程中捕获SIGINT以很好地退出它们.在linux上,我正在使用signal.pause()它,它完美无缺.
在Windows上实现signal.pause()的最佳方法是什么?
我丑陋的解决方案是:
my_queue.get(True, averylongtime)
Run Code Online (Sandbox Code Playgroud)
并在我的信号处理程序中放入my_queue中的内容.请注意,如果我没有指定超时,则不会捕获SIGINT.但我想知道是否有更好的解决方案.
谢谢
我用这个:
#another:
while not self.quit:
# your code
# main
try:
# your code
except KeyboardInterrupt:
another.quit = True
time.sleep(5) # or wait for threading.enumerate() or similar
Run Code Online (Sandbox Code Playgroud)
如果我想让它更强大,比如说,在存在错误的情况下也退出:
except KeyboardInterrupt:
another.quit = True
signal.alarm(5)
time.sleep(6)
Run Code Online (Sandbox Code Playgroud)
这样做的副作用是,您except:或except Exception, e:(这不是您应该做的事情/太多)必须预先添加的每个块except KeyboardInterrupt: raise,以便异常不会被“吃掉”。
| 归档时间: |
|
| 查看次数: |
3128 次 |
| 最近记录: |