0

功能def refresh_image_list(self)def refresh_server_list(self):更新self.images并按self.servers其尊重的顺序。他们在创建对象时没有问题。

我可以用 Python 的多处理进程调用它们,没有任何问题……除了一个……附加到 self.images 和 self.servers 的信息不保留。同样,当我不使用多处理时,这不是问题……只有当我使用多处理进程时。image_list()我通过将函数切换到and来验证这些函数在使用 Process 运行它们时确实运行正确,server_list()并且它们成功打印了列表。但同样,self.servers和 self.images 中的信息之后没有保留。

class Account():
    def __init__(self, username, api, pipe_object):
        self.images = []
        self.servers = []
        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()
        p2.join()
        p1.join()

    flavors = None 


    def refresh_server_list(self):
    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("Active 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):
    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
4

1 回答 1

1

这很明显,因为当您启动一个新进程时,它不共享相同的地址空间并且进程数据被复制,因此您无法从子进程修改主进程中的原始对象。如果您使用可以工作的线程,因为它们共享相同的地址空间。

如果你想修改一个对象,你必须使用Manager类来共享它,或者你可以使用multiprocessing.Array& Co.

或者,您可以使用队列/管道或您在多处理模块中使用的任何内容返回带有某种输出的修改列表,而不是共享列表。

于 2013-08-11T21:55:08.907 回答