I am getting familiar with Python's multiprocessing
module. The following code works as expected:
#outputs 0 1 2 3
from multiprocessing import Pool
def run_one(x):
print x
return
pool = Pool(processes=12)
for i in range(4):
pool.apply_async(run_one, (i,))
pool.close()
pool.join()
Now, however, if I wrap a function around the above code, the print
statements are not executed (or the output is redirected at least):
#outputs nothing
def run():
def run_one(x):
print x
return
pool = Pool(processes=12)
for i in range(4):
pool.apply_async(run_one, (i,))
pool.close()
pool.join()
If I move the run_one
definition outside of run
, the output is the expected one again, when I'm calling run()
:
#outputs 0 1 2 3
def run_one(x):
print x
return
def run():
pool = Pool(processes=12)
for i in range(4):
pool.apply_async(run_one, (i,))
pool.close()
pool.join()
What am I missing here? Why isn't the second snippet printing anything? If I simply call the run_one(i)
function instead of using apply_async
, all the three codes output the same.