我正在尝试编写不断从机器获取数据的python代码,并在图形窗口中绘制这些数据。我正在使用matplotlib启用交互式绘图/ ion()。
有相当多的数据,因此绘图可能需要一段时间。由于python代码在更新绘图之前不会继续,因此在更新绘图时停止数据采集。
我希望避免因更新图表而导致的数据差距。是否有一种(简单可靠的)方法来更新图形/图形而不阻止代码的执行,直到在屏幕上更新图形为止?
编辑23.9.2015:
我按照以下建议尝试了线程。我把它放在一个脚本中:
import threading
import matplotlib.pyplot as plt
def plotter():
print 'Starting plot...'
plt.plot([1,2,3,4])
plt.show()
print '...plot done.'
return
t = threading.Thread(target=plotter)
t.start()
执行此脚本会导致崩溃(使用MacPorts的Python 2.7的Mac OS X;请参阅下文)。如果我注释掉plt.plot(...)和plt.show(),那么该脚本可以顺利运行并且符合预期。有什么帮助或建议有什么问题?
$ python plot_in_own_thread.py
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
Starting plot...
2016-09-23 08:43:19.433 Python[89176:39798237] *** Assertion failure in +[NSUndoManager _endTopLevelGroupings], /Library/Caches/com.apple.xbs/Sources/Foundation/Foundation-1259/Misc.subproj/NSUndoManager.m:359
2016-09-23 08:43:19.433 Python[89176:39798237] +[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.
2016-09-23 08:43:19.435 Python[89176:39798237] (
0 CoreFoundation 0x00007fff8cbb14f2 __exceptionPreprocess + 178
1 libobjc.A.dylib 0x00007fff9f7ab73c objc_exception_throw + 48
2 CoreFoundation 0x00007fff8cbb61ca +[NSException raise:format:arguments:] + 106
3 Foundation 0x00007fff9ba2d856 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
4 Foundation 0x00007fff9b9b2af1 +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 170
5 AppKit 0x00007fff98061e22 -[NSApplication run] + 844
6 _macosx.so 0x000000010d3494a2 show + 210
7 Python 0x000000010aff2539 PyEval_EvalFrameEx + 27929
8 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
9 Python 0x000000010aff6e36 fast_function + 118
10 Python 0x000000010aff23b8 PyEval_EvalFrameEx + 27544
11 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
12 Python 0x000000010af771cc function_call + 364
13 Python 0x000000010af514c3 PyObject_Call + 99
14 Python 0x000000010af5e526 instancemethod_call + 182
15 Python 0x000000010af514c3 PyObject_Call + 99
16 Python 0x000000010afac5fb slot_tp_call + 171
17 Python 0x000000010af514c3 PyObject_Call + 99
18 Python 0x000000010aff2c8c PyEval_EvalFrameEx + 29804
19 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
20 Python 0x000000010aff6e36 fast_function + 118
21 Python 0x000000010aff23b8 PyEval_EvalFrameEx + 27544
22 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
23 Python 0x000000010af771cc function_call + 364
24 Python 0x000000010af514c3 PyObject_Call + 99
25 Python 0x000000010aff2c8c PyEval_EvalFrameEx + 29804
26 Python 0x000000010aff6f16 fast_function + 342
27 Python 0x000000010aff23b8 PyEval_EvalFrameEx + 27544
28 Python 0x000000010aff6f16 fast_function + 342
29 Python 0x000000010aff23b8 PyEval_EvalFrameEx + 27544
30 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
31 Python 0x000000010af771cc function_call + 364
32 Python 0x000000010af514c3 PyObject_Call + 99
33 Python 0x000000010af5e526 instancemethod_call + 182
34 Python 0x000000010af514c3 PyObject_Call + 99
35 Python 0x000000010aff68b5 PyEval_CallObjectWithKeywords + 165
36 Python 0x000000010b030cb6 t_bootstrap + 70
37 libsystem_pthread.dylib 0x00007fff9b92b99d _pthread_body + 131
38 libsystem_pthread.dylib 0x00007fff9b92b91a _pthread_body + 0
39 libsystem_pthread.dylib 0x00007fff9b929351 thread_start + 13
)
2016-09-23 08:43:19.436 Python[89176:39798237] *** Assertion failure in +[NSUndoManager _endTopLevelGroupings], /Library/Caches/com.apple.xbs/Sources/Foundation/Foundation-1259/Misc.subproj/NSUndoManager.m:359
2016-09-23 08:43:19.438 Python[89176:39798237] An uncaught exception was raised
2016-09-23 08:43:19.438 Python[89176:39798237] +[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.
2016-09-23 08:43:19.438 Python[89176:39798237] (
0 CoreFoundation 0x00007fff8cbb14f2 __exceptionPreprocess + 178
1 libobjc.A.dylib 0x00007fff9f7ab73c objc_exception_throw + 48
2 CoreFoundation 0x00007fff8cbb61ca +[NSException raise:format:arguments:] + 106
3 Foundation 0x00007fff9ba2d856 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
4 Foundation 0x00007fff9b9b2af1 +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 170
5 AppKit 0x00007fff98061ebe -[NSApplication run] + 1000
6 _macosx.so 0x000000010d3494a2 show + 210
7 Python 0x000000010aff2539 PyEval_EvalFrameEx + 27929
8 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
9 Python 0x000000010aff6e36 fast_function + 118
10 Python 0x000000010aff23b8 PyEval_EvalFrameEx + 27544
11 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
12 Python 0x000000010af771cc function_call + 364
13 Python 0x000000010af514c3 PyObject_Call + 99
14 Python 0x000000010af5e526 instancemethod_call + 182
15 Python 0x000000010af514c3 PyObject_Call + 99
16 Python 0x000000010afac5fb slot_tp_call + 171
17 Python 0x000000010af514c3 PyObject_Call + 99
18 Python 0x000000010aff2c8c PyEval_EvalFrameEx + 29804
19 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
20 Python 0x000000010aff6e36 fast_function + 118
21 Python 0x000000010aff23b8 PyEval_EvalFrameEx + 27544
22 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
23 Python 0x000000010af771cc function_call + 364
24 Python 0x000000010af514c3 PyObject_Call + 99
25 Python 0x000000010aff2c8c PyEval_EvalFrameEx + 29804
26 Python 0x000000010aff6f16 fast_function + 342
27 Python 0x000000010aff23b8 PyEval_EvalFrameEx + 27544
28 Python 0x000000010aff6f16 fast_function + 342
29 Python 0x000000010aff23b8 PyEval_EvalFrameEx + 27544
30 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
31 Python 0x000000010af771cc function_call + 364
32 Python 0x000000010af514c3 PyObject_Call + 99
33 Python 0x000000010af5e526 instancemethod_call + 182
34 Python 0x000000010af514c3 PyObject_Call + 99
35 Python 0x000000010aff68b5 PyEval_CallObjectWithKeywords + 165
36 Python 0x000000010b030cb6 t_bootstrap + 70
37 libsystem_pthread.dylib 0x00007fff9b92b99d _pthread_body + 131
38 libsystem_pthread.dylib 0x00007fff9b92b91a _pthread_body + 0
39 libsystem_pthread.dylib 0x00007fff9b929351 thread_start + 13
)
2016-09-23 08:43:19.438 Python[89176:39798237] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8cbb14f2 __exceptionPreprocess + 178
1 libobjc.A.dylib 0x00007fff9f7ab73c objc_exception_throw + 48
2 CoreFoundation 0x00007fff8cbb61ca +[NSException raise:format:arguments:] + 106
3 Foundation 0x00007fff9ba2d856 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
4 Foundation 0x00007fff9b9b2af1 +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 170
5 AppKit 0x00007fff98061ebe -[NSApplication run] + 1000
6 _macosx.so 0x000000010d3494a2 show + 210
7 Python 0x000000010aff2539 PyEval_EvalFrameEx + 27929
8 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
9 Python 0x000000010aff6e36 fast_function + 118
10 Python 0x000000010aff23b8 PyEval_EvalFrameEx + 27544
11 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
12 Python 0x000000010af771cc function_call + 364
13 Python 0x000000010af514c3 PyObject_Call + 99
14 Python 0x000000010af5e526 instancemethod_call + 182
15 Python 0x000000010af514c3 PyObject_Call + 99
16 Python 0x000000010afac5fb slot_tp_call + 171
17 Python 0x000000010af514c3 PyObject_Call + 99
18 Python 0x000000010aff2c8c PyEval_EvalFrameEx + 29804
19 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
20 Python 0x000000010aff6e36 fast_function + 118
21 Python 0x000000010aff23b8 PyEval_EvalFrameEx + 27544
22 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
23 Python 0x000000010af771cc function_call + 364
24 Python 0x000000010af514c3 PyObject_Call + 99
25 Python 0x000000010aff2c8c PyEval_EvalFrameEx + 29804
26 Python 0x000000010aff6f16 fast_function + 342
27 Python 0x000000010aff23b8 PyEval_EvalFrameEx + 27544
28 Python 0x000000010aff6f16 fast_function + 342
29 Python 0x000000010aff23b8 PyEval_EvalFrameEx + 27544
30 Python 0x000000010afeb52a PyEval_EvalCodeEx + 1690
31 Python 0x000000010af771cc function_call + 364
32 Python 0x000000010af514c3 PyObject_Call + 99
33 Python 0x000000010af5e526 instancemethod_call + 182
34 Python 0x000000010af514c3 PyObject_Call + 99
35 Python 0x000000010aff68b5 PyEval_CallObjectWithKeywords + 165
36 Python 0x000000010b030cb6 t_bootstrap + 70
37 libsystem_pthread.dylib 0x00007fff9b92b99d _pthread_body + 131
38 libsystem_pthread.dylib 0x00007fff9b92b91a _pthread_body + 0
39 libsystem_pthread.dylib 0x00007fff9b929351 thread_start + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6