Why is the Python multiprocessor running sequentially?

I have a really simple code below. #! / USR / bin / python from multiprocessor import pool import time

def worker(job): if job in range(25,30): time.sleep(10) print "job:%s" %job return (job) pool = Pool(processes=10) result = [] for job in range(1, 1000): result.append(pool.apply_async(worker(job))) pool.close() pool.join() 

As you can see, I have a worker to handle 1000 jobs using multiprocessing. If the task is 25-30, the worker will sleep 10 seconds. This is an attempt to simulate the task of spending time and resources.

When I run the above code, the output looks like below. From task 25. The whole process works as a sequential process. Since every 10 seconds there is a result after task 24. Until task 30 is completed.

But why? Should a multiprocessor process work simultaneously?

 [ root@localhost tmp]# ./a.py job:1 job:2 job:3 job:4 job:5 job:6 job:7 job:8 job:9 job:10 job:11 job:12 job:13 job:14 job:15 job:16 job:17 job:18 job:19 job:20 job:21 job:22 job:23 job:24 job:25 job:26 ... 
+5
source share
1 answer

Because you call it when you instantiate. You should pass the called and the arguments, not the result, apply_async.

 result.append(pool.apply_async(worker, [job])) 
+4
source

Source: https://habr.com/ru/post/1240434/


All Articles