我一直在研究一个问题(我是一名化学工程师,所以我需要永远理解如何编写代码)如何让几个标签在他们自己的过程中运行,但每个标签都有它的自己的数据显示在matplotlib
图中。
我遇到了很多酸洗错误,我想知道是否有人有任何简单的解决方案。我认为酸洗错误的主要原因是由于我试图将属性作为属性传递给制表符对象。该对象包含一些数据以及许多其他对象,这些对象有助于适应它所拥有的数据。
我觉得这些物品非常漂亮而且非常必要,但我也意识到它们会引起酸洗问题。
这是我的代码的一个非常简化的版本:
(如果你想复制/粘贴以测试它,它仍然会编译。)
import multiprocessing as mp
from PyQt4 import QtGui, QtCore
import numpy as np
import matplotlib
matplotlib.use('QtAgg')
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib import figure
import sys
import lmfit
# This object will just hold certain objects which will help create data objects ato be shown in matplotlib plots
# this could be a type of species with properties that could be quantized to a location on an axis (like number of teeth)
#, which special_object would hold another quantization of that property (like length of teeth)
class object_within_special_object:
def __init__(self, n, m):
self.n = n
self.m = m
def location(self, i):
location = i*self.m/self.n
return location
def NM(self):
return str(self.n) + str(self.m)
# This is what will hold a number of species and all of their properties,
# as well as some data to try and fit using the species and their properties
class special_object:
def __init__(self, name, X, Y):
self.name = name
self.X = X
self.Y = Y
self.params = lmfit.Parameters()
self.things = self.make_a_whole_bunch_of_things()
for thing in self.things:
self.params.add('something' + str(thing.NM()) + 's', value = 3)
def make_a_whole_bunch_of_things(self):
things = []
for n in range(0,20):
m=1
things.append(object_within_special_object(n,m))
return things
# a special type of tab which holds a (or a couple of) matplotlib plots and a special_object ( which holds the data to display in those plots)
class Special_Tab(QtGui.QTabWidget):
def __init__(self, parent, special_object):
QtGui.QTabWidget.__init__(self, parent)
self.special_object = special_object
self.grid = QtGui.QGridLayout(self)
# matplotlib figure put into tab
self.fig = figure.Figure()
self.plot = self.fig.add_subplot(111)
self.line, = self.plot.plot(self.special_object.X, self.special_object.Y, 'r-')
self.canvas = FigureCanvas(self.fig)
self.grid.addWidget(self.canvas)
self.canvas.show()
self.canvas.draw()
self.canvas_BBox = self.plot.figure.canvas.copy_from_bbox(self.plot.bbox)
ax1 = self.plot.figure.axes[0]
def process_on_special_object(self):
# do a long fitting process involving the properties of the special_object
return
def update_GUI(self):
# change the GUI to reflect changes made to special_object
self.line.set_data(special_object.X, special_object.Y)
self.plot.draw_artist(self.line)
self.plot.figure.canvas.blit(self.plot.bbox)
return
# This window just has a button to make all of the tabs in separate processes
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
# This GUI stuff shouldn't be too important
QtGui.QMainWindow.__init__(self)
self.resize(int(app.desktop().screenGeometry().width()*.6), int(app.desktop().screenGeometry().height()*.6))
self.tabs_list = []
central_widget = QtGui.QWidget(self)
self.main_tab_widget = QtGui.QTabWidget()
self.layout = QtGui.QHBoxLayout(central_widget)
button = QtGui.QPushButton('Open Tabs')
self.layout.addWidget(button)
self.layout.addWidget(self.main_tab_widget)
QtCore.QObject.connect(button, QtCore.SIGNAL("clicked()"), self.open_tabs)
self.setCentralWidget(central_widget)
central_widget.setLayout(self.layout)
# Here we open several tabs and put them in different processes
def open_tabs(self):
for i in range(0, 10):
# this is just some random data for the objects
X = np.arange(1240.0/1350.0, 1240./200., 0.01)
Y = np.array(np.e**.2*X + np.sin(10*X)+np.cos(4*X))
# Here the special tab is created
new_tab = Special_Tab(self.main_tab_widget, special_object(str(i), X, Y))
self.main_tab_widget.addTab(new_tab, str(i))
# this part works fine without the .start() function
self.tabs_list.append(mp.Process(target=new_tab))
# this is where pickling errors occur
self.tabs_list[-1].start()
return
if __name__ == "__main__":
app = QtGui.QApplication([])
win = MainWindow()
win.show()
sys.exit(app.exec_())
我注意到错误来自matplotlib轴(我不知道怎么做?)并给出错误pickle.PicklingError: Can't pickle <class 'matplotlib.axes.AxesSubplot'>: it's not found as matplotlib.axes.AxesSubplot
。另外,我注意到注释掉matplotlib图也会给出酸洗错误pickle.PicklingError: Can't pickle <function <lambda> at 0x012A2B30>: it's not found as lmfit.parameter.<lambda>
。我认为这是因为lambda函数无法被腌制,我想lmfit在它的深处某处有一个lambda ...但我真的不知道如何处理这些错误。
奇怪的是,我看到的原始代码(不是这里显示的简化版本)的错误略有不同,但情绪基本相同。我在其他代码中遇到的错误是pickle.PicklingError: Can't pickle 'BufferRegion' object: <BufferRegion object at 0x037EBA04>
有没有人能够更好地解决这个问题,方法是将对象移动到我传递的位置或任何其他想法?
我非常感谢您的时间和精力以及对此问题的任何帮助。
编辑:我在某种程度上尝试了unutbu的想法,但对流程功能的位置进行了一些改动。
所提出的解决方案的唯一问题是do_long_fitting_process()
函数调用另一个函数,该函数迭代地更新matplotlib
图中的行。因此do_long_fitting_process()需要具有对Special_Tab
属性的一些访问权限才能更改它们并显示GUI的更新。
我已经尝试通过将do_long_fitting_process()
函数推送到一个全局函数并调用它来执行此操作:
[代码] def open_tabs(self): 对于范围内的i(0,10): ... self.tabs_list.append(new_tab)
consumer, producer = mp.Pipe()
process = mp.Process(target=process_on_special_object, args=(producer,))
process.start()
while(True):
message = consumer.recv()
if message == 'done':
break
tab.update_GUI(message[0], message[1])
process_list[-1].join()
[/代码]
我通过update_GUI()
将数据传递给mp.Pipe()
的地方,但是一旦我启动流程,窗口就会转到“无响应”。
答案 0 :(得分:1)
问题是并非Axes
对象的所有部分都可以序列化,这对于在进程之间移动数据是必要的。我建议稍微重新组织你的代码,将计算推送到单独的进程(任何需要超过几分之一秒的事情),但要保留你所有的绘图。主要流程,只来回传递数据。
我们QThread
的另一个选择是time.sleep() required to keep QThread responsive?有两种不同的实现方法(问题中有一个,我的答案中有一个)。
答案 1 :(得分:1)
将GUI代码与计算代码分开。 GUI必须在单个进程中运行(尽管它可能会产生多个线程)。让计算代码包含在special_object
中。
当你想要执行长时间运行的计算时,让Special_Tab
调用mp.Process
:
class special_object:
def do_long_fitting_process(self):
pass
class Special_Tab(QtGui.QTabWidget):
def process_on_special_object(self):
# do a long fitting process involving the properties of the
# special_object
proc = mp.Process(target = self.special_object.do_long_fitting_process)
proc.start()
class MainWindow(QtGui.QMainWindow):
def open_tabs(self):
for i in range(0, 10):
...
self.tabs_list.append(new_tab)
new_tab.process_on_special_object()