我在信號處理程序中更改全局變量並在主程序中輪詢它。但是這個值在主線程中並沒有改變。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感謝:) –