@ senderle 's here 외에도 일부 사용자는의 기능을 사용하는 방법을 궁금해 할 수도 있습니다 multiprocessing.Pool
.
좋은 점은 최상위 수준의 모든 친숙한 API를 모방 .Pool()
하는 manager
인스턴스 메서드 가 있다는 것입니다 multiprocessing
.
from itertools import repeat
import multiprocessing as mp
import os
import pprint
def f(d: dict) -> None:
pid = os.getpid()
d[pid] = "Hi, I was written by process %d" % pid
if __name__ == '__main__':
with mp.Manager() as manager:
d = manager.dict()
with manager.Pool() as pool:
pool.map(f, repeat(d, 10))
# `d` is a DictProxy object that can be converted to dict
pprint.pprint(dict(d))
산출:
$ python3 mul.py
{22562: 'Hi, I was written by process 22562',
22563: 'Hi, I was written by process 22563',
22564: 'Hi, I was written by process 22564',
22565: 'Hi, I was written by process 22565',
22566: 'Hi, I was written by process 22566',
22567: 'Hi, I was written by process 22567',
22568: 'Hi, I was written by process 22568',
22569: 'Hi, I was written by process 22569',
22570: 'Hi, I was written by process 22570',
22571: 'Hi, I was written by process 22571'}
이것은 각 프로세스가 해당 프로세스 ID를 전역 DictProxy
개체에 기록하는 약간 다른 예 d
입니다.