Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

python multiprocessing for loop is not running for all loop arguments

Here is the code I am trying to do. The output text file should contain 500 data. But it is always less than 500 (450 or 476 or 429 when I run). Any idea why this is happening and what should I do to get 500 data in output. It will be very helpful if I get output in order.

def foo(j):        
        output=[j]        
        f=open('output.txt','a')
        f.write('
')
        
        np.savetxt(f,output)
        f.close()



if __name__=='__main__':
    pool = Pool(processes=4)
    pool.map(foo,range(500)) 
question from:https://stackoverflow.com/questions/65859354/python-multiprocessing-for-loop-is-not-running-for-all-loop-arguments

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try creating chunks before hand. For example

def f_amp(inputs):
    chunks = [inputs for inputs in range(500)]
 
    pool = Pool(processes=4)
 
    result = pool.map(f, chunks)

Also you can refer here for solutions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...