如何在程序结束时关闭 python 进程
how to close python process on program end
我无法在程序结束时正确关闭进程中 运行 的串行连接。 (在 windows/VSCode 和 Ctrl-C 上)
我收到一条错误消息,大多数情况下该端口已在程序下次启动时打开。
我必须先完成 运行 过程吗?
class serialOne(Process):
def __init__(self, serial_port, debug, baudrate=57600, timeout=1):
...
def terminate(self):
print("close ports")
self.active = False
self.ser.close()
def run(self):
while self.active:
self.initCom()
self.readCom()
time.sleep(0.005)
def main():
global processList
global debug
while True:
if debug == True:
print("main")
time.sleep(1.0)
for process in processList:
process.terminate()
和我的主要:
def main():
global processList
global debug
while True:
if debug == True:
print("main") # actually doing nothing
time.sleep(1.0)
for process in processList:
process.terminate()
这是错误信息:
Process serialOne-1:
Traceback (most recent call last):
File "C:\Users\dgapp\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
Traceback (most recent call last):
File "e:\_python\rfid_jacky\simple_multiprocess_rfid_02.py", line 129, in run
time.sleep(0.005)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\ptvsd_launcher.py", line 45, in <module>
KeyboardInterrupt
main(ptvsdArgs)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 265, in main
wait=args.wait)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 258, in handle_args
debug_main(addr, name, kind, *extra, **kwargs)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 45, in debug_main
run_file(address, name, *extra, **kwargs)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 79, in run_file
run(argv, addr, **kwargs)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 140, in _run
_pydevd.main()
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1925, in main
debugger.connect(host, port)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1283, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1290, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "e:\_python\rfid_jacky\simple_multiprocess_rfid_02.py", line 161, in <module>
main()
File "e:\_python\rfid_jacky\simple_multiprocess_rfid_02.py", line 140, in main
time.sleep(1.0)
KeyboardInterrupt
当您按下 Ctrl+C
时,将抛出 KeyboardInterrupt
异常,并中断您的无限睡眠循环。但是由于您没有捕获到此异常,因此永远不会调用此循环(使用 process.terminate()
)之后的代码 after,这可能会导致您的问题。
所以你有几个选择:
捕获 KeyboardInterrupt
并使用它来退出无限循环:
def main():
global processList
global debug
try:
while True:
if debug == True:
print("main") # actually doing nothing
time.sleep(1.0)
except KeyboardInterrupt:
pass
for process in processList:
process.terminate()
简单易读。
注册一个退出处理程序,它将在您的程序退出时 运行:
import atexit
@atexit.register
def shutdown():
global processList
for process in processList:
process.terminate()
def main():
global debug
while True:
if debug == True:
print("main") # actually doing nothing
time.sleep(1.0)
哪个更可靠,因为即使您的进程被另一个信号终止,它也会工作。
当用户按下中断键时会发生 KeyboardInterrupt。
一个简单的解决方案是捕获异常:
while True:
if debug == True:
print("main") # actually doing nothing
try:
# do your things
except KeyboardInterrupt:
print("program was interrupted by user")
break
您也可以使用 finally
关键字来正确结束您的程序:
try:
while True:
# do your things
except KeyboardInterrupt:
print("program was interrupted by user")
break
finally:
close() # this will always happen, even if an exception was raised
我无法在程序结束时正确关闭进程中 运行 的串行连接。 (在 windows/VSCode 和 Ctrl-C 上)
我收到一条错误消息,大多数情况下该端口已在程序下次启动时打开。
我必须先完成 运行 过程吗?
class serialOne(Process):
def __init__(self, serial_port, debug, baudrate=57600, timeout=1):
...
def terminate(self):
print("close ports")
self.active = False
self.ser.close()
def run(self):
while self.active:
self.initCom()
self.readCom()
time.sleep(0.005)
def main():
global processList
global debug
while True:
if debug == True:
print("main")
time.sleep(1.0)
for process in processList:
process.terminate()
和我的主要:
def main():
global processList
global debug
while True:
if debug == True:
print("main") # actually doing nothing
time.sleep(1.0)
for process in processList:
process.terminate()
这是错误信息:
Process serialOne-1:
Traceback (most recent call last):
File "C:\Users\dgapp\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
Traceback (most recent call last):
File "e:\_python\rfid_jacky\simple_multiprocess_rfid_02.py", line 129, in run
time.sleep(0.005)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\ptvsd_launcher.py", line 45, in <module>
KeyboardInterrupt
main(ptvsdArgs)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 265, in main
wait=args.wait)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 258, in handle_args
debug_main(addr, name, kind, *extra, **kwargs)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 45, in debug_main
run_file(address, name, *extra, **kwargs)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 79, in run_file
run(argv, addr, **kwargs)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 140, in _run
_pydevd.main()
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1925, in main
debugger.connect(host, port)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1283, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1290, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "c:\Users\dgapp\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "e:\_python\rfid_jacky\simple_multiprocess_rfid_02.py", line 161, in <module>
main()
File "e:\_python\rfid_jacky\simple_multiprocess_rfid_02.py", line 140, in main
time.sleep(1.0)
KeyboardInterrupt
当您按下 Ctrl+C
时,将抛出 KeyboardInterrupt
异常,并中断您的无限睡眠循环。但是由于您没有捕获到此异常,因此永远不会调用此循环(使用 process.terminate()
)之后的代码 after,这可能会导致您的问题。
所以你有几个选择:
捕获
KeyboardInterrupt
并使用它来退出无限循环:def main(): global processList global debug try: while True: if debug == True: print("main") # actually doing nothing time.sleep(1.0) except KeyboardInterrupt: pass for process in processList: process.terminate()
简单易读。
注册一个退出处理程序,它将在您的程序退出时 运行:
import atexit @atexit.register def shutdown(): global processList for process in processList: process.terminate() def main(): global debug while True: if debug == True: print("main") # actually doing nothing time.sleep(1.0)
哪个更可靠,因为即使您的进程被另一个信号终止,它也会工作。
当用户按下中断键时会发生 KeyboardInterrupt。
一个简单的解决方案是捕获异常:
while True:
if debug == True:
print("main") # actually doing nothing
try:
# do your things
except KeyboardInterrupt:
print("program was interrupted by user")
break
您也可以使用 finally
关键字来正确结束您的程序:
try:
while True:
# do your things
except KeyboardInterrupt:
print("program was interrupted by user")
break
finally:
close() # this will always happen, even if an exception was raised