Senior
How to exchange data between processes in Python?
sobes.tech AI
Answer from AI
In Python, for inter-process communication, you can use the multiprocessing module, which provides several mechanisms:
- Queues — thread-safe queues that allow processes to safely exchange objects.
from multiprocessing import Process, Queue
def worker(q):
q.put('Data from child process')
if __name__ == '__main__':
q = Queue()
p = Process(target=worker, args=(q,))
p.start()
print(q.get()) # Receive data
p.join()
-
Pools with return values — you can get results from processes using
apply_asyncmethods. -
Shared objects (Value, Array) — allow creating objects of primitive types shared between processes.
-
Managers — allow creating more complex shared objects, such as dictionaries and lists.
from multiprocessing import Process, Manager
def worker(d):
d['key'] = 'value'
if __name__ == '__main__':
with Manager() as manager:
d = manager.dict()
p = Process(target=worker, args=(d,))
p.start()
p.join()
print(d) # {'key': 'value'}
- Sockets or other IPC mechanisms — for more complex scenarios, you can use sockets, pipes, or files.
The choice depends on the task: for simple data exchange, Queue or Manager are the best options.