函数def refresh_image_list(self)
和def refresh_server_list(self)
:以尊重的顺序更新self.images
和self.servers
。它们在创建对象时没有任何问题。
我可以使用Python的多处理过程调用它们,没有问题以太...除了一个......附加到self.images和self.servers的信息不会保留。同样,当我不使用多处理时,这不是问题......只有当我使用多处理过程时才会这样。我通过将函数切换到image_list()
和server_list()
并通过成功打印出列表来验证函数在使用Process运行时正确运行。但同样,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
答案 0 :(得分:1)
这很明显,因为当您启动新流程时,它不会共享相同的地址空间,并且流程数据被复制,因此您无法修改原始对象子进程的主要过程。如果您使用的线程可以使用,因为它们共享相同的地址空间。
如果您要修改对象,则必须使用Manager
课程进行分享,或者使用multiprocessing.Array
&有限公司
或者,您可以使用queue/pipe或您在多处理模块中熟悉的任何内容,而不是共享列表,而是使用某种输出返回 new 修改后的列表。