How do I pass large numpy arrays between python subprocesses without saving to disk?

后端 未结 6 1481
说谎
说谎 2020-11-29 21:21

Is there a good way to pass a large chunk of data between two python subprocesses without using the disk? Here\'s a cartoon example of what I\'m hoping to accomplish:

<
6条回答
  •  悲哀的现实
    2020-11-29 21:47

    While googling around for more information about the code Joe Kington posted, I found the numpy-sharedmem package. Judging from this numpy/multiprocessing tutorial it seems to share the same intellectual heritage (maybe largely the same authors? -- I'm not sure).

    Using the sharedmem module, you can create a shared-memory numpy array (awesome!), and use it with multiprocessing like this:

    import sharedmem as shm
    import numpy as np
    import multiprocessing as mp
    
    def worker(q,arr):
        done = False
        while not done:
            cmd = q.get()
            if cmd == 'done':
                done = True
            elif cmd == 'data':
                ##Fake data. In real life, get data from hardware.
                rnd=np.random.randint(100)
                print('rnd={0}'.format(rnd))
                arr[:]=rnd
            q.task_done()
    
    if __name__=='__main__':
        N=10
        arr=shm.zeros(N,dtype=np.uint8)
        q=mp.JoinableQueue()    
        proc = mp.Process(target=worker, args=[q,arr])
        proc.daemon=True
        proc.start()
    
        for i in range(3):
            q.put('data')
            # Wait for the computation to finish
            q.join()   
            print arr.shape
            print(arr)
        q.put('done')
        proc.join()
    

    Running yields

    rnd=53
    (10,)
    [53 53 53 53 53 53 53 53 53 53]
    rnd=15
    (10,)
    [15 15 15 15 15 15 15 15 15 15]
    rnd=87
    (10,)
    [87 87 87 87 87 87 87 87 87 87]
    

提交回复
热议问题