下面是我想做的示例代码:
class myfiles:
def __init__:
self.input_file = ""
self.output_file = ""
class process_files:
def __init__(self, list_of_myfiles):
self.myfiles_list = list_of_myfiles
self.my_new_files = []
def intial_work(self):
list_of_objects = []
for f in self.myfiles_list:
new_files = myfiles()
new_files.input_file = f
list_of_objects.append(new_files)
self.my_new_files = list_of_objects
def update_files(self):
# this will update the instance output_file
for f in self.my_new_files:
# do some work on input file and write it to ouput new_files
process_files.work_function(f)
# this will not update the instance output_file
n_cores = psutil.cpu_count()
p = multiprocessing.Pool(n_cores)
p.map(process_files.work_function, self.my_new_files)
@staticmethod
def work_function(new_file):
# do some work on input file and update output
new_file.output_file = "something new"
我有两个类第一个是myfiles
,我将它用作对象列表。
第二个是process_files
,它将处理前一个类中的对象。你知道吗
list_of_files = ["a", "b", "c"]
myprocess = process_files(list_of_files)
myprocess.intial_work()
myprocess.update_files()
在方法update_files()
中,如果我使用for循环来更新实例myfiles.output_file
将有一个值,但是如果我使用Pool.map myfiles.output_file
将始终是空字符串,为什么?你知道吗
如果游泳池不起作用。有没有其他方法来并行运行它们?(多线程或多处理)
目前没有回答
相关问题 更多 >
编程相关推荐