【发布时间】:2017-01-30 02:01:17
【问题描述】:
我正在尝试编写不断从机器获取数据并将这些数据绘制在图形窗口中的 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
【问题讨论】:
标签: python asynchronous matplotlib background blocking