Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Multiprocessing iterates over script

I am trying my hands upon multiprocessing.pool, and it seems to work fine, except that whole script loops again and again.....

My code -

from multiprocessing import Pool

print "at top!"

arr = [30, 30, 30, 30]

def fib(num):
    if num == 0 or num == 1: return 1
    else: return (fib(num - 1) + fib(num - 2))

def main():
    print "start"
    pool = Pool(processes=4)
    result = [pool.apply_async(fib, args=(arr[i],)) for i in range(4)]
    pool.close()
    pool.join()
    for res in result:
        print res.get()
    print "end"


if __name__ == '__main__':
    main()


print "end of program ?"

And what it returns is this -

at top!
start
at top!
end of program ?
at top!
end of program ?
at top!
end of program ?
at top!
end of program ?
1346269
1346269
1346269
1346269
end
end of program ?

What I don't want is the repeating at top ! and end of program.

I want the main program to freeze at that point and continue only after all the computations are finished.

PS. I am running it on windows 10, 64 - bit. Python 2.7 32 - bit.

Thanks in advance.

like image 352
devojoyti Avatar asked Jun 15 '17 06:06

devojoyti


1 Answers

that's expected if you're running windows, the way that python windows spawns the processes: the script is imported as many times as there are spawned processes. So you cannot have only one "at top" print if you put unindented (at script top level). Same goes for print "end of program ?" of course.

(this doesn't happen on Linux which explains the comments of users saying that they cannot reproduce)

This is also why you had to protect your main with if __name__ == '__main__' test. So to get the expected output you'll have to do:

if __name__ == '__main__':
    print("at top!")
    main()
    print("end of program")
like image 119
Jean-François Fabre Avatar answered Oct 20 '22 13:10

Jean-François Fabre