Python multiprocessing apply_async never returns result on Windows 7 -
Python multiprocessing apply_async never returns result on Windows 7 -
i trying follow simple multiprocessing example:
import multiprocessing mp def cube(x): homecoming x**3 pool = mp.pool(processes=2) results = [pool.apply_async(cube, args=x) x in range(1,7)]
however, on windows machine, not able result (on ubuntu 12.04lts runs perfectly).
if inspect results
, see following:
[<multiprocessing.pool.applyresult object @ 0x01ff0910>, <multiprocessing.pool.applyresult object @ 0x01ff0950>, <multiprocessing.pool.applyresult object @ 0x01ff0990>, <multiprocessing.pool.applyresult object @ 0x01ff09d0>, <multiprocessing.pool.applyresult object @ 0x01ff0a10>, <multiprocessing.pool.applyresult object @ 0x01ff0a50>]
if run results[0].ready()
false
.
if run results[0].get()
python interpreter freezes, waiting result never comes.
the illustration simple gets, thinking low level bug relating os (i on windows 7). perhaps else has improve idea?
there couple of mistakes here. first, must declare pool
within if __name__ == "__main__":
guard when running on windows. second, have pass args
keyword argument sequence, if you're passing 1 argument. putting together:
import multiprocessing mp def cube(x): homecoming x**3 if __name__ == "__main__": pool = mp.pool(processes=2) results = [pool.apply_async(cube, args=(x,)) x in range(1,7)] print([result.get() result in results])
output:
[1, 8, 27, 64, 125, 216]
edit:
oh, moarningsun mentions, multiprocessing
does not work well in interactive interpreter:
note
functionality within bundle requires __main__
module importable children. covered in programming guidelines worth pointing out here. means examples, such multiprocessing.pool
examples not work in interactive interpreter.
so you'll need execute code script test properly.
python windows multiprocessing
Comments
Post a Comment