我在信号处理程序中更改全局变量并在主程序中轮询它。但是这个值在主线程中并没有改变。Python - 轮询变量
是否有一个限定符,我需要使用它来使其变成一个易变(如Java)变量?
这里的程序:
test.py每当我按
import time
import signal
def debug():
closeSession = False
def sigint_handler(signal, frame):
global closeSession
print('Breaking the poll...')
closeSession=True
signal.signal(signal.SIGINT, sigint_handler)
# Start a program...
while not closeSession:
time.sleep(1)
print('Polling... closeSession = %r' % closeSession)
print('Exiting! Bye.')
# Sent 'quit' to stdin of the program
if __name__ == "__main__":
debug()
sigint_handler()
被称为按Ctrl +ç但的closeSession
新的值不会在主线程中使用。
我得到以下输出:
$蟒蛇test.py
投票... closeSession =假
投票... closeSession =假
我按下Ctrl键 + C
^CBreaking投票...
投票... closeSession =假
按下Ctrl键+Ç,再次
^CBreaking投票..
Polling ... closeSession = False
按下Ctrl键+Ç,再次
^CBreaking投票...
投票... closeSession =假
投票... closeSession =假
非常好的格式**第一个问题**。 –
@MohitJain感谢:) –