多处理管理器共享列表的Python Pickle错误

时间:2013-08-11 23:44:20

标签: python multiprocessing pickle

我正在尝试使用多处理功能self.refresh_images_list和self.refresh_server_list填充列表self.images和self.servers。我这样做,所以当创建对象时,它们将启动异步。我正在使用共享列表,因此子副本将更新原始对象列表。

但是,我得到了一个Pickle错误,所以我很困难。

class Account():
    def __init__(self, username, api, pipe_object):
        manager = Manager()
        self.images = manager.list()
        self.servers = manager.list()
        self.images_timestamp = None
        self.servers_timestamp = None
        #needed a dictionary instead of 
        #list/tuple. This works best for 
        #the generator.
        self.regions = {
                "DFW" : pyrax.connect_to_cloudservers("DFW"),
                "ORD" : pyrax.connect_to_cloudservers("ORD"),
                "SYD" : pyrax.connect_to_cloudservers("SYD")
                }
    p1 = Process(target = self.refresh_server_list, args=())
    p2 = Process(target = self.refresh_image_list, args=())
    p1.start()
    p2.start()
    p1.join()
    p2.join()

    flavors = None
    #multiprocessing shares lists only for __init__
    #after __init__, we want to break the share
    unshare_lists = False


    def refresh_server_list(self):
    if self.unshare_lists:
        self.servers = []
        self.servers_timestamp = time.strftime(
            "%I:%M:%S", time.localtime()
        )
        with Redirect(self.pipe_object):
            print "\nRefreshing server cache...hold on!"

        for region, value in self.regions.iteritems():
            region_servers = value.servers.list()
            for region_servers in generator(region_servers, region):
                self.servers.append(region_servers)

        with Redirect(self.pipe_object):
            print "\nServer cache completed!"

    def server_list(self):
        if not self.servers:
            self.refresh_server_list()

        with Redirect(self.pipe_object):
            print_header("Cached Server List", "-")
            for srv in self.servers:
                print "\nName: %s" % srv.name
                print "Created: %s" % srv.created
                print "Progress: %s" % srv.progress
                print "Status: %s" % srv.status
                print "URL: %s" % srv.links[0]["href"]
                print "Networks: %s" % srv.networks
            print "\nLast Refresh time: %s" % self.servers_timestamp

    def refresh_image_list(self):
    if self.unshare_lists:
        self.images = []
        self.images_timestamp = time.strftime(
            "%I:%M:%S", time.localtime()
        )
        with Redirect(self.pipe_object):
           # print_header("Active Image List", "-")
            print "\nRefreshing image cache...hold on!"

        for region, value in self.regions.iteritems():
            region_images = value.images.list()
            for region_images in generator(region_images, region):
                self.images.append(region_images)

        with Redirect(self.pipe_object):
            print "\nImage cache completed!"

    def image_list(self):
        if not self.images:
            self.refresh_image_list()

        with Redirect(self.pipe_object):
            print_header("List Images", "-")
            for img in self.images:
                print (
                    str(self.images.index(img)+1) + ") "
                    + "Name: %s\n    ID: %s Status: %s" %
                    (img.name, img.id, img.status)
                    )
            print "\nLast Refresh time: %s" % self.images_timestamp

我得到的错误:

Refreshing server cache...hold on!
Traceback (most recent call last):
  File "menu.py", line 162, in <module>
    main()
  File "menu.py", line 156, in main
    menus[value](hash_table, accounts)
  File "menu.py", line 104, in menu
    choices[value]()
  File "/home/work/modules/classes.py", line 87, in server_list
    self.refresh_server_list()
  File "/home/work/modules/classes.py", line 80, in refresh_server_list
    self.servers.append(region_servers)
  File "<string>", line 2, in append
  File "/usr/lib64/python2.7/multiprocessing/managers.py", line 758, in _callmethod
    conn.send((self._id, methodname, args, kwds))
cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed

0 个答案:

没有答案