Reference Address: http://blog.sina.com.cn/s/blog_60bf8fe901017x12.html
The following while loop, at run time will always occupy the terminal, and do not receive normal input, then what is the method? The big guy may immediately think of CTRL + C directly stop, admittedly, this is OK, no problem.
# !/usr/local/bin/python # -*-coding:utf-8-*- while True: Print " start ... .... "
But here's the problem:
If a program, he will do 2 things, one is to constantly scroll the output, the other is acceptable input operation, and 2 things need to be interactive under a process, how to do? Well, directly on the solution, let Python directly at the CTRL + C signal processing to meet our needs of the situation
#!/usr/local/bin/python#-*-coding:utf-8-*-ImportRe,sysImportstringImportSignaldefSigint_handler (Signum, frame):Globalis_sigint_up is_sigint_up=TruePrint 'catched Interrupt signal!'signal.signal (signal. SIGINT, Sigint_handler) signal.signal (signal. SIGHUP, Sigint_handler) signal.signal (signal. SIGTERM, Sigint_handler) is_sigint_up=False whileTrue:Try: #the things you want to do Import TimePrint "start ... ....."Time.sleep (2) ifis_sigint_up:#code that needs to be processed when interrupted Print "Exit"is_sigint_up=FalseContinue exceptexcepting,e: Break
Python handles Ctrl + C method