Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在scipy.integrate.odeint中处理键盘中断_Python_Exception Handling_Scipy_Numerical Integration - Fatal编程技术网

Python 在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.odeint时,是否有合理的方法来处理求导过程中的异常

如果在紧密循环中调用积分器,并且出于某种原因需要停止执行,则此问题变得非常重要。目前,我已经求助于将
键盘中断
转换为
系统退出
异常(见下文)以使进程退出,这很好,但显然并不理想

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。在这种情况下,请在提交一份错误报告。如果没有解决方法,它不能按预期工作,则可能是一个错误。在这种情况下,请在