Creating a pool of workers
Creating a processing pool of worker processes is generally a difficult task. You need to take care of scheduling jobs, processing the queue, handling the processes, and the most difficult part, handling synchronization between the processes without too much overhead.
With multiprocessing however, these problems have been solved already. You can simply create a process pool with a given number of processes and just add tasks to it whenever you need to. The following is an example of a multiprocessing version of the map operator and demonstrates that processing will not stall the application:
import time
import multiprocessing
def busy_wait(n):
while n > 0:
n -= 1
if __name__ == '__main__':
n = 10000000
items = [n for _ in range(8)]
with multiprocessing.Pool() as pool:
results = []
start = time.time()
print('Start processing...')
for _ in range(5):
results.append(pool.map_async...