#!/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()
            Asked
            
        
        
            Active
            
        
            Viewed 1,430 times
        
    1
            
            
         
    
    
        halfelf
        
- 9,737
- 13
- 54
- 63
 
    
    
        snowflake01986
        
- 21
- 1
- 3
- 
                    Are you running the code from IPython console or as a script file? – amanb Jul 19 '19 at 08:00
- 
                    is your issue resolved? – amanb Dec 23 '19 at 10:05
1 Answers
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