Pickling objects

2020-05-09 09:03发布

I need to pickle object [wxpython frame object] and send it as a prameter to this function apply_async on multiproccessing pool module could someone provide me an example how can I do it I tried the following and get an error message :

myfile = file(r"C:\binary.dat", "w")
pickle.dump(self, myfile)
myfile.close()


self.my_pool.apply_async(fun,[i,myfile])

def fun(i,self_object):
    window = pickle.load(self_oject)
    wx.CallAfter(window.LogData, msg)

could someone tell me what could be the problem

If the error give some indicator below the last error message i get: File "C:\Python26\lib\copy_reg.py", line 70, in _reduce_ex raise TypeError, "can't pickle %s objects" % base.name TypeError: can't pickle PySwigObject objects

2条回答
Evening l夕情丶
2楼-- · 2020-05-09 09:32

I don't believe that wxPython objects can be pickled. They are just wrappers around C objects, which contain lots of pointers and other stateful stuff. The pickle module doesn't know enough about them to be able to restore their state afterwards.

查看更多
来,给爷笑一个
3楼-- · 2020-05-09 09:37

You can not serialize a widget for use in another process. I guess you want to change the GUI content from another process that is started by the multiprocessing module. In that case, you should define a callback function in the parent process that gets called when the result of the sub-process is ready. Therefore you can use the "callback" parameter of apply_async.

Something like:

def fun(i):
    # do something in this sub-process and then return a log message
    return "finished doing something"

def cb(resultFromFun):
    wx.CallAfter(window.LogData, resultFromFun)

my_pool.apply_async(fun, [i], callback = cb)
查看更多
登录 后发表回答