title: Sharing data between processes without physically moving it
body: <p>I have a job where I get a lot of separate tasks through. For each task I need to download some data, process it and then upload it again.</p>
  
  <p>I'm using a multiprocessing pool for the processing.</p>
  
  <p>I have a couple of issues I'm unsure of though.</p>
  
  <p>Firstly the data can be up to 20MB roughly, I ideally want to get it to the child worker process without physically moving it in memory and getting the resulting data back to the parent process as well without moving it. As I'm not sure how some tools are working under the hood I don't know if I can just pass the data as an argument to the pool's <code>apply_async</code> (from my understanding it serialises the objects and then they're created again once the reach the subprocess?), or if I should use a multiprocessing <code>Queue</code> or <code>mmap</code> maybe? or something else?</p>
  
  <p>I looked at <a href="http://docs.python.org/2/library/multiprocessing.html#shared-ctypes-objects" rel="nofollow">ctypes objects</a> but from what I understand only the objects that are defined when the pool is created when the process forks can be shared? Which is no good for me as I'll continuously have new data coming in which I need to share.</p>
  
  <p>One thing I shouldn't need to worry about is any concurrent access on the data so I shouldn't need any type of locking. This is because the processing will only start after the data has been downloaded, and the upload will also only start after the output data has been generated.</p>
  
  <p>Another issue I'm having is that sometimes the tasks coming in might spike and as a result I'm downloading data for the tasks quicker than the child processes can process it. So therefore I'm downloading data quicker than I can finish the tasks and dispose of the data and python is dying from running out of memory. What would be a good way to hold up the tasks at the downloading stage when memory is almost full / too much data is in the job pipeline?
  I was thinking of some type of "ref" count by using the number of data bytes so I can limit the amount of data between download and upload and only download when the number is below some threshold. Although I'd be worried a child might sometimes fail and I'd never get to take the data it had off of the count. Is there a good way to achieve this kind of thing?</p>