我在使用 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 (thewith
statement)
- Comprehensions (eitherlist
,dict
,set
or generator comprehensions)
- Generator delegation (yield
from)
Theraise
statement is supported in several forms:
raise
(to re-raise the current exception)
raise SomeException
raise SomeException(<arguments>)
: innopython
mode, constructor arguments must be compile-time constants
Similarly, theassert
statement is supported with or without an error message.
关于python - Numba 和 KeyboardInterrupt 异常冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33108075/