Sobes.tech
Junior — Middle

Is it possible to create mechanisms for information exchange between separate processes on a multi-core system?

sobes.tech AI

Answer from AI

Yes, in Python, it is possible to create mechanisms for exchanging information between separate processes on a multi-core system.

This is done using the multiprocessing module, which provides objects for inter-process communication:

  • Queue — a thread-safe queue for passing data between processes.
  • Pipe — a bidirectional communication channel between two processes.
  • Manager — an object that allows creating shared objects (lists, dictionaries, etc.) accessible from different processes.

Example of using Queue:

from multiprocessing import Process, Queue

def worker(q):
    q.put('Hello from the child process')

if __name__ == '__main__':
    q = Queue()
    p = Process(target=worker, args=(q,))
    p.start()
    print(q.get())  # Receive message from the child process
    p.join()

Thus, processes can exchange data safely and efficiently.