In fact, you will have 8instances SomeKindOfClass(one for each worker), but only 4 will be active at a time.
multiprocessing vs multiprocessing.dummy
, multiprocessing.dummy, threading. " python" ( ). " Python" ; "" . Python GIL, - python , , python.
from multiprocessing import Pool, , allOutputs , ( , , , , , , main()). , multiprocessing . allOutputs, . , , .
, - , .
, , , - Manager
import multiprocessing
def worker(args):
index, array = args
a = SomeKindOfClass()
a.some_expensive_function()
array[index] = a.output
def main():
n = 8
manager = multiprocessing.Manager()
array = manager.list([0] * n)
pool = multiprocessing.Pool(4)
pool.map(worker, [(i, array) for i in range(n)])
print array