[英]Running different function parallel in python3.7 using multiprocessing
I want to run two different function in parallel in python, I have used the below code:我想在 python 中并行运行两个不同的 function,我使用了以下代码:
def remove_special_char(data):
data['Description'] = data['Description'].apply(lambda val: re.sub(r'^=', "'=", str(val))) # Change cell values which start with '=' sign leading to Excel formula issues
return(data)
file_path1 = '.\file1.xlsx'
file_path2 = '.\file2.xlsx'
def method1(file_path1):
data = pd.read_excel(file_path1)
data= remove_special_char(data)
return data
def method2(file_path2):
data = pd.read_excel(file_path2)
data= remove_special_char(data)
return data
I am using the below Pool
process, but its not working.我正在使用下面的Pool
进程,但它不起作用。
from multiprocessing import Pool
p = Pool(3)
result1 = p.map(method1(file_path1), args=file_path1)
result2 = p.map(method2(file_path1), args=file_path2)
I want to run both these methods in parallel to save execution time and at the same time get the return value as well.我想并行运行这两种方法以节省执行时间,同时也获得返回值。
I don't know why you are defining the same method twice with different parameter names, but anyway the map
method of Pool
s is taking as its first argument a function, and the second argument is an iterable.我不知道你为什么用不同的参数名称定义相同的方法两次,但无论如何Pool
s 的map
方法将 function 作为其第一个参数,第二个参数是可迭代的。 What map
does is call the function on each item of the iterable, and return a list with all the results. map
所做的是在迭代的每个项目上调用 function,并返回一个包含所有结果的列表。 So what you want to do is more something like:所以你想要做的更像是:
from multiprocessing import Pool
file_paths = ('.\file1.xlsx', '.\file2.xlsx')
def method(file_path):
data = pd.read_excel(file_path)
data= remove_special_char(data)
return data
with Pool(3) as p:
result = p.map(method, file_paths)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.