Python › Concurrency (asyncio/threading/multiprocessing)
How do you share data between processes using multiprocessing, given that separate processes don't share memory the way threads do?
AdvancedAnswer
Use multiprocessing's dedicated inter-process communication mechanisms: a Queue or Pipe for passing data/messages between processes, or shared memory constructs like Value/Array (or multiprocessing.shared_memory for larger data) for genuinely SHARED, synchronized state — you can't just share a regular Python object directly, since each process has its own independent memory space.
from multiprocessing import Process, Queue
def worker(q):
q.put("result from worker")
if __name__ == "__main__":
q = Queue()
p = Process(target=worker, args=(q,))
p.start()
p.join()
print(q.get()) # "result from worker" -- data explicitly passed via the QueueReal-world example Collecting results from multiple parallel worker processes back into the main process for aggregation.