我有一个使用PyRun_SimpleFileEx C应用程序接口执行Python脚本的C++代码。 py文件如下所示 import time
time.sleep(2)
raise SystemExit("Sorry, it is time to exit.") SystemExit也是通过调用quit()或abort()之类的python内容而引发的 因为SystemExit继承自BaseException而不是Exception,所以它不会被捕获并关闭Python环境和C++应用程序。 有没有办法从C++捕获上面的异常? 提前感谢所有最好的MB
在python中,以下语法通常是处理异常的方法-- SystemExit是可能的。
try:
execute some command here
except SystemExit:
take a different course of action here
但是,我想使用异常处理的方式,以不采取其他的行动方针。一般来说,我只想阻止SystemExit并继续执行程序。我尝试了以下方法,但只有在for循环中才能起作用
try:
execute some command here
except SystemExit:
continue
如何执行这个通用代码块?
我有一组代码如下所示:
class MigrationsCreatedTest(TestCase):
def test_migrations_created(self):
try:
call_command("makemigrations", "--check", "--dry-run")
except SystemExit:
raise Exception("There are migrations that have not been create
示例:
function! MyFunc()
do this
do that
python3 << endpython
if var = "something":
return
else:
do this
endpython
endfunction
return命令不起作用:
E880:无法在vim中处理python异常的SystemExit
使用exit()或quit()时也是如此
如何从python代码中退出vim函数?
在我的python3脚本中,我用subprocess模块调用一个外部程序。当被调用的程序以非零状态代码退出时,将引发CalledProcessError。在这种情况下,我打印一条错误消息,并希望在异常处理程序中终止脚本。
我的问题是,exit()通过抛出SystemExit异常本身来工作,所以我最终得到了:
During handling of the above exception, another exception occurred:
脚本看起来类似于以下内容:
try:
output = subprocess.check_output(["program"])
Gunicorn有时会使我的服务器崩溃,实际上是用sys.exit(1)退出Python解释器!为什么会这样呢?注意,故障并不总是在同一点上。在下面所示的两种情况下,有一个不同的最后代码线在古尼科恩的exit之前。这里运行的代码是openpyxl,它不应该导致解释器关闭!
服务器内存用完了吗?其他原因?
(这是中码头上的Gunicorn上的酒瓶。)
案例1
File "/virtualenv_for_docker/lib/python3.7/site-packages/openpyxl/descriptors/base.py", line 166, in __set__
我在Python 3.2.3中有以下脚本:
try:
file = open('file.txt', 'r')
except IOError:
print('There was an error opening the file!')
sys.exit()
#more code that is relevant only if the file exists
如果文件不存在(或者只是打开文件时出错),我该如何优雅地退出?
我可以使用exit(),但这会打开一个对话框面板,询问我是否要终止应用程序。
我可以使用sys.ex
不幸的是,在php /nginx配置上调用php exit()并不会立即停止脚本,而文件句柄可能不会关闭。
一些开发人员建议调用fastcgi_finish_request(),但这并不能阻止脚本。
其他人建议将所有代码包装在catch块中:
<?php
class SystemExit extends Exception {}
try {
/* PUT ALL THE CODE HERE */
} catch (SystemExit $e) { /* do nothing */ }
?>
并在需要停止代码的情况下抛出异常:
if (SOME_
我正在使用C++将第三方包集成到python应用程序。包通过网络连接到专有API并接收更新。总的流程是python实例化一个C++对象,调用它的函数来设置它,然后等待更新。
我使用实现了更新的回调机制,在从python或python调用的C++函数进行测试时,它工作得很好。也就是说,我能够继承Python中的C++类,从C++调用它的虚拟函数,并看到python代码获得优先级并执行。
问题:
当我从网络接收到更新时,我得到:
The thread 'Win32 Thread' (0x1f78) has exited with code 0 (0x0).
Unhandled ex
我有一个阻塞功能,我需要在一定时间后杀死它。但出于“原因”,我不得不使用python2.7 (对不起)。我正在使用来实现我所需要的/
def fn(v):
try:
for i in range(v):
print(i)
time.sleep(1)
except:
print('exc!')
raise
print('done...')
t = threading.Thread(target=fn, args=(5,))
t.start()