1
#!/usr/bin/env python3.5
import multiprocessing, time

def testfuncxx(num):
    time.sleep(num)
    print(num


pool = multiprocessing.Pool(processes=3)

)

for i in range(10):
    #testfuncxx(i)
    #print(i, '=======')
    pool.apply_async(testfuncxx, args=(i,))

pool.close()
pool.join()
halfelf
  • 9,737
  • 13
  • 54
  • 63

1 Answers1

0

Since I can only test on Windows, the code works when I enclose the code in a if __name__ == '__main__' entry-point protection. More details here. In general, including this protection is recommended in the multiprocessing guidelines.

Note: The code was run as a script test.py. To run the code in IPython or Jupyter notebook, you may import it like import test.

test.py:

import multiprocessing, time

def testfuncxx(num):
    time.sleep(num)    
    print(num)

def apply_async_callback():  
    pool = multiprocessing.Pool(processes=3) 
    for i in range(10):  
        pool.apply_async(testfuncxx, args=(i,))
    pool.close()
    pool.join()  


if __name__=='__main__':    
    apply_async_callback()

#Output:
0
1
2
3
4
5
6
7
8
9
amanb
  • 5,276
  • 3
  • 19
  • 38