python - Numba 和 KeyboardInterrupt 异常冲突

标签 python exception python-3.4 jit numba

我在使用 Continuum Numba 模块编译 KeyboardInterrupt 异常时遇到问题。这是我的数据采集代码:

@jit
def lockinmeasurement(x):
    Measurement=np.empty((0,5))
    XMeas=np.empty((0,2))
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path_to_watch, recursive=True)
    observer.start()
    try:
       while x:
           SQData=pd.read_csv(path_to_watch,sep=',',skiprows=14)
           Temp=SQData['Temperature (K)']
           Field=SQData['Field (Oe)']
           XMeas=np.append(XMeas,[[time.clock(),lockin.x]],axis=0)
           Measurement=np.append(Measurement,[[a,b,c,d,e]])
           p1.plot(XMeas,clear=True,label='Lockin X',pen='y')
           pg.QtGui.QApplication.processEvents()    
           rd=pd.DataFrame(Measurement)
           rd.to_csv('fileout.csv',sep='\t',index=False)
           time.sleep(0.2)
    except KeyboardInterrupt:
       print('interrupted!')

lockinmeasurement(True)

代码在没有“@jit”的情况下编译得很好,但是当我尝试使用numba时它会返回一个错误:

Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\numba\bytecode.py", line 231, in next
    info = BYTECODE_TABLE[opcode]
KeyError: 121

During handling of the above exception, another exception occurred:

lockinmeasurement(True)

  File "C:\Anaconda3\lib\site-packages\numba\dispatcher.py", line 165, in _compile_for_args
    return self.compile(sig)

  File "C:\Anaconda3\lib\site-packages\numba\dispatcher.py", line 303, in compile
    flags=flags, locals=self.locals)

  File "C:\Anaconda3\lib\site-packages\numba\compiler.py", line 595, in compile_extra
    return pipeline.compile_extra(func)

  File "C:\Anaconda3\lib\site-packages\numba\compiler.py", line 316, in compile_extra
    raise e

  File "C:\Anaconda3\lib\site-packages\numba\compiler.py", line 311, in compile_extra

    bc = self.extract_bytecode(func)
  File "C:\Anaconda3\lib\site-packages\numba\compiler.py", line 303, in extract_bytecode

    bc = bytecode.ByteCode(func=self.func)

  File "C:\Anaconda3\lib\site-packages\numba\bytecode.py", line 333, in __init__
    table = utils.SortedMap(ByteCodeIter(code))

  File "C:\Anaconda3\lib\site-packages\numba\utils.py", line 109, in __init__
    for i, (k, v) in enumerate(sorted(seq)):

  File "C:\Anaconda3\lib\site-packages\numba\bytecode.py", line 235, in next
    raise NotImplementedError(ts % tv)

NotImplementedError: offset=80 opcode=0x79 opname=SETUP_EXCEPT

有什么办法可以解决这个问题吗?

最佳答案

感谢Travis创新团队的努力numba是一个伟大而强大的科学计算工具。然而,应在可行的情况下谨慎使用它 jit -编译可以给我们艰苦而快速的生活带来一些成果。

Numba 文档明确指出了这一点:

2.4.1.1. Constructs

Numba strives to support as much of the Python language as possible, but some language features are not available inside Numba-compiled functions:

- Function definition

- Class definition

- Exception handling (try .. except, try .. finally)

- Context management (the with statement)

- Comprehensions (either list, dict, set or generator comprehensions)

- Generator delegation (yield from)


The raise statement is supported in several forms:

raise (to re-raise the current exception)
raise SomeException
raise SomeException(<arguments>): in nopython mode, constructor arguments must be compile-time constants

Similarly, the assert statement is supported with or without an error message.

关于python - Numba 和 KeyboardInterrupt 异常冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33108075/

相关文章:

python - 获取django模板中的模板名称

python - 在 numpy 数组中查找相同的行和列

Java异常处理——捕捉父类(super class)异常

python - 我可以在 Celery 任务中使用通用的 logging.Logger() 吗?

python - 从 python 调用 BCP 抛出超时异常,然后立即完成

datetime.now() 的 Python 解析

python - 当我构建 Docker 文件时,出现错误 The 'make' utility is missing from PATH

python - Django RSS 提要问题

c++ - 按值或引用抛出异常

java - 何时使用扩展 RuntimeException 的用户定义类?