Several commonly used methods for inter-process communication in Python

The following methods are commonly used for inter-process communication in Python:

1. Queue (Queue) Multiple processes use queues for data exchange. Processes send and receive objects through queues.

A queue is a data structure that can store any type of data and supports multi-threaded operations. Therefore, in Python's multi-process programming, queues can be used to implement inter-process communication.

The following is a simple example code that uses queues to implement inter-process communication:

import multiprocessing

def producer(queue):
    for i in range(10):
        queue.put(i)

def consumer(queue):
    while True:
        item = queue.get()
        if item is None:
            break
        print(item)

if __name__ == '__main__':
    queue = multiprocessing.Queue()

    p1 = multiprocessing.Process(target=producer, args=(queue,))
    p2 = multiprocessing.Process(target=consumer, args=(queue,))

    p1.start()
    p2.start()

    p1.join()
    queue.put(None)
    p2.join()

In this sample code, we define two functions producer and consumer, which are used to write data to the queue and read data from the queue respectively. We initialized a queue and then created two processes p1 and p2. p1 is used to execute the producer function and p2 is used to execute the consumer function.

2. Pipe (Pipe) Pipe can be used for communication between processes with affinity, such as one-way communication between two processes.

Pipes are also a way of inter-process communication. Pipes are divided into ordinary pipes (which can only be used between parent and child processes) and named pipes (which can be used between multiple processes).

The following is a simple example code that uses pipes to implement inter-process communication:

import multiprocessing

def producer(pipe):
    for i in range(10):
        pipe.send(i)

    pipe.close()

def consumer(pipe):
    while True:
        try:
            item = pipe.recv()
            print(item)
        except EOFError:
            break

if __name__ == '__main__':
    pipe = multiprocessing.Pipe()

    p1 = multiprocessing.Process(target=producer, args=(pipe[0],))
    p2 = multiprocessing.Process(target=consumer, args=(pipe[1],))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

In this sample code, we create a pipeline, and then start two processes p1 and p2, p1 is used to execute the producer function, and p2 is used to execute the consumer function. We send data to the pipeline in the producer function, and then close the pipeline; in the consumer function, we continue to read data from the pipeline until we encounter an EOFError exception.

3. Shared memory processes can define shared memory segments, and multiple processes can access the same memory space to achieve data sharing. Synchronization mechanisms such as Value or Array need to be used.

Shared memory is a very efficient inter-process communication method, which allows multiple processes to access the same memory area, so as to achieve the purpose of data sharing.

The following is a simple example code that uses shared memory to implement inter-process communication:

import multiprocessing

def producer(shared_value):
    for i in range(10):
        shared_value.value = i

def consumer(shared_value):
    while True:
        print(shared_value.value)

if __name__ == '__main__':
    shared_value = multiprocessing.Value('i', 0)

    p1 = multiprocessing.Process(target=producer, args=(shared_value,))
    p2 = multiprocessing.Process(target=consumer, args=(shared_value,))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

In this sample code, we define two functions producer and consumer, the producer function is used to write data to the shared memory, and the consumer function is used to read data from the shared memory. We use multiprocessing.Value to create a shared memory object and pass it to both processes. In the producer function, we constantly change the value of the shared memory, and in the consumer function, we read the value of the shared memory in a loop and print it out.

4. Message Queue (Message Queue) Put messages into the queue through the message queue interface, and then get messages from the queue, and the queue passes the messages to other processes.

5. Semaphore (Semaphore) is used for signal transmission between processes and between different threads of the same process. It can send signals and receive signals.

Semaphores can be used for synchronization control between multiple processes, including process mutual exclusion and process synchronization.

The following is a simple example code that uses semaphores to implement inter-process communication:

import multiprocessing

def producer(queue, sem):
    for i in range(10):
        sem.acquire()
        queue.put(i)

def consumer(queue, sem):
    while True:
        item = queue.get()
        print(item)
        sem.release()

if __name__ == '__main__':
    queue = multiprocessing.Queue()
    sem = multiprocessing.Semaphore(1)

    p1 = multiprocessing.Process(target=producer, args=(queue, sem))
    p2 = multiprocessing.Process(target=consumer, args=(queue, sem))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

In this sample code, we define two functions, producer and consumer. The producer function is used to write data to the queue, and the consumer function is used to read data from the queue. We use multiprocessing.Semaphore to create a semaphore and set its initial value to 1. In the producer function, we apply for the semaphore through the sem.acquire() method. If it can be applied for, we write data to the queue, otherwise we wait indefinitely; in the consumer function, we read the data from the queue and pass sem. The release() method releases the semaphore.

6. Socket Processes can communicate through network sockets, just like clients and servers through network interfaces.

By choosing the appropriate communication mechanism, data exchange and communication can be carried out between Python processes. The multiprocessing module provides various components that support inter-process communication.

Guess you like

Origin blog.csdn.net/weixin_53909748/article/details/132202694