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?

Advanced

Answer

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 Queue
Real-world example Collecting results from multiple parallel worker processes back into the main process for aggregation.

Common follow-ups: Why is a regular Python list or dict NOT automatically shared between multiprocessing.Process instances the way it would be between threads?