多线程时的酸洗错误 - 设计或代码问题?

时间:2012-07-27 09:30:01

标签: python multiprocessing

我正在pygtk中编写一个工具,它需要处理递归解析大型目录,并将结果匹配的文件添加到列表中。此过程显然会导致用户界面挂起,因此我尝试使用多处理库。

在我要求解决方案之前还有一些背景知识: - 该程序有两个主要的类,一个控制器类,它完成所有密集的工作并与UI通信,还有一个Model类,用于处理工具所需的所有数据。

import sys
import os
import pygtk  
import fnmatch
from multiprocessing import Pool
pygtk.require("2.0")  

#try:  
from gi.repository import Gtk
from gi.repository import GObject
#except:  
#   print("GTK Not Availible")
#   sys.exit(1)


class Controller(object):
    def __init__(self,builder,model):
        self.builder=builder
        self.model=model
    def btn_pass_clicked(self, *args,**kwargs):
        print "it's working!, its woooooorkkinnnnggg!"
        spinnywheel= self.builder.get_object("activitySpinner")
        spinnywheel.start()
    def btn_fail_clicked(self, *args, **kwargs):
        print "stopping spinnywheel!"
        spinnywheel=self.builder.get_object("activitySpinner")
        spinnywheel.stop()
    def quit(self,*args,**kwargs):
        print "iamquit"
        Gtk.main_quit()
    def file_menu_open(self,*args,**kwargs):
        print "file->open"
        self.builder.get_object("openDialogue").show()
    def opendialogue_btnOpen_clicked(self,*args,**kwargs):
        rootdir = os.path.expanduser(self.builder.get_object("openDialogue_entryBox").get_text())
        self.builder.get_object("openDialogue").hide()
        self.builder.get_object("openDialogue_entryBox").set_text("")
        if os.path.exists(rootdir):
            self.builder.get_object("activitySpinner").start()
            print "pooling workers and walking ",rootdir
            p = Pool(None)
            p.apply_async(self.walk_for_files,rootdir,None,self.finished_recurse)
        else:
            print "Path does not exist!"


    def walk_for_files(self,rootdir):
            for root,dirs,files in os.walk(rootdir):
                    for extension in ['c','cpp']:
                        for filename in fnmatch.filter(files,'*.'+extension):
                            self.model.add_single_file(os.path.join(root,filename))

    def finished_recurse(self,*args,**kargs):
        print "workers finished parsing dirs!"
        self.builder.get_object("activitySpinner").stop()


class Model(object):
    def __init__(self):
        self.fileList=[]

    def add_single_file(self,file):
        self.fileList.append(file)
        print "added ",file




class Scrutiny(object):
    def __init__(self):
        builder = Gtk.Builder()
        builder.add_from_file("scrutinydev.ui")
        model_object=Model()
        controller_object=Controller(builder,model_object)
        builder.connect_signals(controller_object)
        builder.get_object("windowMain").show()
        builder.get_object("listView")
        GObject.threads_init()
        Gtk.main()



if __name__ == "__main__":
    scrutiny = Scrutiny()

现在,继续我的问题。

正如您所看到的,使用pool()生成的worker需要执行回调finish_recurse,以便我可以在其他UI工作中停止GtkSpinner。

当代码处于当前状态时,我得到一个酸洗错误,

PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed

我理解这是因为我无法序列化回调,并且希望得到变通方法/解决方案的建议以实现我的需要。

1 个答案:

答案 0 :(得分:0)

我不太了解GTK,但我认为你的问题更多的是关于酸洗而不是多处理。

pickle module的__getstate__和__setstate__方法可让您自定义任何对象的酸洗过程。

这是一个简单的例子,展示了它的工作原理:

from pickle import dumps, loads


class NotPickable(object):
    def __init__(self, x):
        self.attr = x

ffile = open('/tmp/filesarenotpickable', 'r+w')    
o = NotPickable(ffile)
dumps(o)
# =>  TypeError: can't pickle file objects

class Pickable(NotPickable):
    attr = open('/tmp/a_file_on_an_other_system', 'r+w')

    def __getstate__(self):
        return self.attr.read()

    def __setstate__(self, state):
        self.attr.write(state)

o = Pickable(ffile)                                            
dumps(o)
# OUT: 'ccopy_reg\n_reconstructor\np0\n(c__main__\nPickable\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n.'                        

o2 = loads(dumps(o))                                           
o2.attr
# OUT: <open file '/tmp/a_file_on_an_other_system', mode 'r+w' at 0x18ad4b0>

当然,开发人员有责任表示并正确恢复对象的状态。