重复How to prevent a block of code from being interrupted by KeyboardInterrupt in Python?
目的:
问题:
我的设计的通用实现:
import threading, signal
def huphandler(signum, frame):
with sig_lock:
os._exit(0)
def loop():
print 'in lock'
time.sleep(5)
def idle_loop():
while True:
with sig_lock:
t = multiprocessing.Process(target=loop)
t.start()
t.join()
print 'out lock'
time.sleep(5)
sig_lock = threading.Lock()
signal.signal(signal.SIGHUP, huphandler)
idle_loop()
答案 0 :(得分:0)
如果你使用获得的锁杀死,那么你将程序置于死锁状态。
因为您刚刚杀死了持有锁的线程或子进程,所以它无法再释放它。然后你的主线程或任何其他线程将尝试获取锁,并阻塞直到锁可用。因此,以僵局结束。
答案 1 :(得分:0)
1.1。问题:
1.2。解决方案:
2.1。问题:
time.sleep
),这是不稳定的。
2.2。解决方案:
提供所需结果的通用实施:
import threading, signal
def huphandler(signum, frame):
print idle_loop.sig_lock
if idle_loop.sig_lock:
idle_loop.continuee = False
else:
print 'dont b here'
os._exit(0)
def loop():
print 'in lock'
time.sleep(5)
def idle_loop():
while True:
idle_loop.sig_lock = True
t = multiprocessing.Process(target=loop)
t.start()
t.join()
print 'out lock'
if idle_loop.continuee:
idle_loop.sig_lock = False
time.sleep(5)
else: return
idle_loop.sig_lock = False
idle_loop.continuee = True
signal.signal(signal.SIGHUP, huphandler)
idle_loop()