Python 在scipy.integrate.odeint中处理键盘中断
在调用scipy.integrate.odeint时,是否有合理的方法来处理求导过程中的异常 如果在紧密循环中调用积分器,并且出于某种原因需要停止执行,则此问题变得非常重要。目前,我已经求助于将Python 在scipy.integrate.odeint中处理键盘中断,python,exception-handling,scipy,numerical-integration,Python,Exception Handling,Scipy,Numerical Integration,在调用scipy.integrate.odeint时,是否有合理的方法来处理求导过程中的异常 如果在紧密循环中调用积分器,并且出于某种原因需要停止执行,则此问题变得非常重要。目前,我已经求助于将键盘中断转换为系统退出异常(见下文)以使进程退出,这很好,但显然并不理想 scipy.integrate绑定和Fortran代码似乎在对odepack.error进行一些操作,但我不确定这些异常是否会传播到Python中 from scipy.integrate import odeint import
键盘中断
转换为系统退出
异常(见下文)以使进程退出,这很好,但显然并不理想
scipy.integrate绑定和Fortran代码似乎在对odepack.error进行一些操作,但我不确定这些异常是否会传播到Python中
from scipy.integrate import odeint
import numpy
import sys
import time
def derivative_faststop(func):
def impl(*args, **kwargs):
try:
return func(*args, **kwargs)
except KeyboardInterrupt:
print sys.exc_info()
sys.exit(1)
return impl
@derivative_faststop
def f(x, t):
# NB. the sleep is only present to model the real system
time.sleep(0.5)
return [1.0]
ts = numpy.linspace(0.00, 1.0, 11)
print odeint(f, [0.0], ts)
如果没有解决办法,它不能像预期的那样工作,那可能是一个bug。在这种情况下,请在提交一份错误报告。如果没有解决方法,它不能按预期工作,则可能是一个错误。在这种情况下,请在