How to use inter-process communication in Python

王林
Release: 2023-10-18 12:00:42
Original
1792 people have browsed it

How to use inter-process communication in Python

How to use inter-process communication in Python

Inter-Process Communication (IPC, Inter-Process Communication) is an important concept in computer science, which allows different Processes exchange data and share resources within the same computer system. In Python, there are many ways to achieve inter-process communication. This article will introduce three common methods: Pipe, Shared Memory and Message Queue, and give specific code examples. .

1. Using Pipe(Pipe)

Pipeline is a one-way communication mechanism that can create a pipeline between the parent process and the child process to realize inter-process communication between them.

In Python, you can create a new pipeline through the multiprocessing.Pipe() function. The following is a sample code that uses pipes for inter-process communication:

from multiprocessing import Process, Pipe

def sender(conn):
    conn.send("Hello from sender!")
    conn.close()

def receiver(conn):
    msg = conn.recv()
    print("Received message:", msg)
    conn.close()

if __name__ == "__main__":
    parent_conn, child_conn = Pipe()
    
    p1 = Process(target=sender, args=(parent_conn,))
    p2 = Process(target=receiver, args=(child_conn,))

    p1.start()
    p2.start()

    p1.join()
    p2.join()
Copy after login

In the above example, we created a pipe, and then created two processes, one for sending messages and one for receiving messages. . Messages can be sent to the pipeline through the conn.send() method, and messages can be received from the pipeline through the conn.recv() method.

2. Use Shared Memory

Shared memory is an efficient way of inter-process communication. It can share a memory area between different processes to achieve data sharing. .

In Python, shared memory can be created through the multiprocessing.Value() and multiprocessing.Array() functions. The following is a sample code that uses shared memory for inter-process communication:

from multiprocessing import Process, Value, Array

def writer(val, arr):
    val.value = 5
    for i in range(len(arr)):
        arr[i] = i * 2

def reader(val, arr):
    print("Value:", val.value)
    print("Array:", arr[:])

if __name__ == "__main__":
    value = Value('i', 0)
    array = Array('i', range(10))

    p1 = Process(target=writer, args=(value, array))
    p2 = Process(target=reader, args=(value, array))

    p1.start()
    p2.start()

    p1.join()
    p2.join()
Copy after login

In the above example, we created a Value object and an Array object, respectively Used to share an integer and an array of integers. By modifying the values ​​of Value objects and Array objects, data can be shared among multiple processes.

3. Use Message Queue (Message Queue)

Message queue is a common method of inter-process communication, which can facilitate data transfer and synchronization.

In Python, you can use the multiprocessing.Queue() function to create a message queue. The following is a sample code that uses message queues for inter-process communication:

from multiprocessing import Process, Queue

def sender(q):
    q.put("Hello from sender!")

def receiver(q):
    msg = q.get()
    print("Received message:", msg)

if __name__ == "__main__":
    queue = Queue()

    p1 = Process(target=sender, args=(queue,))
    p2 = Process(target=receiver, args=(queue,))

    p1.start()
    p2.start()

    p1.join()
    p2.join()
Copy after login

In the above example, we created a message queue, and then created two processes, one for sending messages and one for Receive messages. Messages can be put into the message queue through the q.put() method, and messages can be obtained from the message queue through the q.get() method.

Summary

This article introduces three methods to implement inter-process communication in Python: pipes, shared memory and message queues, and gives specific code examples. These methods can easily realize data exchange and shared resources between processes. In actual applications, appropriate methods can be selected to implement inter-process communication according to specific needs.

The above is the detailed content of How to use inter-process communication in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template