0

sorry for this question because there are several examples in Stackoverflow. I am writing in order to clarify some of my doubts because I am quite new in Python language.

i wrote a function:

def clipmyfile(inFile,poly,outFile):
... # doing something with inFile and poly and return outFile

Normally I do this:

clipmyfile(inFile="File1.txt",poly="poly1.shp",outFile="res1.txt")
clipmyfile(inFile="File2.txt",poly="poly2.shp",outFile="res2.txt")
clipmyfile(inFile="File3.txt",poly="poly3.shp",outFile="res3.txt")
......
clipmyfile(inFile="File21.txt",poly="poly21.shp",outFile="res21.txt")

I had read in this example Run several python programs at the same time and i can use (but probably i wrong)

from multiprocessing import Pool
p = Pool(21)  # like in your example, running 21 separate processes

to run the function in the same time and speed my analysis

I am really honest to say that I didn't understand the next step.

Thanks in advance for help and suggestion Gianni

4

1 回答 1

1

您提供的示例中使用的映射仅适用于接收一个参数的函数。你可以在这里看到一个解决方案:Python multiprocessing pool.map for multiple arguments

在您的情况下,您要做的是(假设您有 3 个包含文件、策略、输出的数组):

def expand_args(f_p_o):
    clipmyfile(*f_p_o)  

files = ["file1.txt", "file2.txt"] 
polis = ["poli1.txt", "poly2.txt"]
outis = ["out1.txt", "out2.txt"]
len_f = len(files)
p = Pool()
p.map(expand_args, [(files[i], polis[i], outis[i]) for i in xrange(len_f)])
于 2012-10-12T10:46:39.263 回答