multiprocessingメモ

import multiprocessing as mp
import multiprocessing.sharedctypes as mpshared


def worker(idx: int, queue: mp.Queue, shared_counter):
    while True:
        if not queue.get():
            break
        shared_counter[idx] += 1


def main():
    NUM_WORKERS = 64

    shared_counter = mpshared.RawArray('B', NUM_WORKERS)
    queues = [mp.Queue() for _ in range(NUM_WORKERS)]
    workers = [mp.Process(target=worker, args=(i, queues[i], shared_counter)) for i in range(NUM_WORKERS)]
    for w in workers:
        w.start()
    for _ in range(10):
        for q in queues:
            q.put(True)
    for q in queues:
        q.put(False)
    for w in workers:
        w.join()
    print([*shared_counter])


main()