私はしばらくの間、いくつかのタブを独自のプロセスで動作させる方法の問題に取り組んできました (私は化学エンジニアなので、何かをコーディングする方法を理解するのに永遠にかかります)。ただし、各タブには独自のデータがあります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>
。これは、ラムダ関数をピクルできないためだと思います.lmfitにはラムダが奥深くにあると思います...しかし、これらのエラーを回避する方法がないとどうすればよいかわかりません。
奇妙なことに、元のコード (ここに示されている単純化されたバージョンではありません) から見たエラーはわずかに異なりますが、基本的には同じです。他のコードで発生するエラーは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): for i in range(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()
が、プロセスを開始するとすぐにウィンドウが「応答なし」になります。