Junior — Middle
What inter-process communication methods do you know in Python?
sobes.tech AI
Answer from AI
In Python, there are several methods for inter-process communication (IPC):
-
multiprocessingmodule:Queue— a thread-safe queue for passing objects between processes.Pipe— a bidirectional channel for data exchange between two processes.Manager— an object that allows creating shared objects (lists, dictionaries) accessible from different processes.
-
Sockets (
socket) — enable data exchange between processes, even on different machines. -
Shared files and memory:
- Using files for reading and writing data.
multiprocessing.shared_memorymodule (since Python 3.8) for creating shared memory.
-
Signals and OS message queues — lower-level mechanisms, rarely used directly in Python.
Example with multiprocessing.Queue:
from multiprocessing import Process, Queue
def worker(q):
q.put('Hello from child')
if __name__ == '__main__':
q = Queue()
p = Process(target=worker, args=(q,))
p.start()
print(q.get()) # Outputs: Hello from child
p.join()