我有以下代码:
try:
self._collect_persons_status()
except manager.AsteriskManagerError:
# If it is not possible to continue with the collection of initial
# person statuses via Asterisk Manager, end the program with an
# error code.
logger.debug('*** exit with 1!!')
sys.exit(1)
Run Code Online (Sandbox Code Playgroud)
该脚本通过 systemd 处理(作为带循环的守护进程运行)。
从日志中我可以看到*** exit with 1!!已打印,但脚本以代码 0 结束,而不是预期的 1:
我究竟做错了什么?
如果你在脚本顶层有这样的东西,它会阻止sys.exit()退出程序:
try:
<invoke script entry point>
except:
<log the error>
Run Code Online (Sandbox Code Playgroud)
这是因为sys.exit()是通过引发SystemExit异常来实现的。您可以通过更改except:为except Exceptionwhich will not catch SystemExit(以及您可能不想处理的其他一些低级异常)来修复代码。
| 归档时间: |
|
| 查看次数: |
167 次 |
| 最近记录: |