我得到了如下简单的程序:
import threading
import time
import signal
WITH_DEADLOCK = 0
lock = threading.Lock()
def interruptHandler(signo, frame):
print str(frame), 'received', signo
lock.acquire()
try:
time.sleep(3)
finally:
if WITH_DEADLOCK:
print str(frame), 'release'
lock.release()
signal.signal(signal.SIGINT, interruptHandler)
for x in xrange(60):
print time.strftime("%H:%M:%S"), 'main thread is working'
time.sleep(1)
因此,如果您启动该程序并且在 3 秒内按两次 Ctrl+C,则不会出现死锁。每次按 Ctrl + C 时,都会显示正确的行。如果您更改 WITH_DEADLOCK=1 并按 Ctrl+C 两次(3 秒内),则程序将挂起。
有人可以解释为什么打印操作会产生如此大的差异吗?
(我的python版本是2.6.5)