Inter-process communication programming (IPC) and experiments

        Inter-process communication (IPC) method

Table of contents

Socket

pipeline

anonymous pipe

message queue

Shared memory

Signal

remote procedure call


Socket

        Socket programming is a programming method for inter-process communication in a computer network . Socket provides a mechanism for transferring data between processes running on different hosts. By using the Socket API, developers can write client and server applications for sending and receiving data.

        Scope: Socket is usually used to implement CS-based network applications , such as Web servers, chat applications, etc.

        The following is a Python Socket programming example, including a TCP echo server and a TCP client.

        client:

import socket

def main():
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect(('localhost', 12345))

    while True:
        message = input("Enter message to send (or 'exit' to quit): ")
        if message == 'exit':
            break

        client_socket.sendall(message.encode())
        response = client_socket.recv(1024)
        print(f"Received: {response.decode()}")

    client_socket.close()

if __name__ == "__main__":
    main()

# 客户端连接到本地主机的端口12345上的服务器。
# 客户端从用户输入获取消息,将其发送到服务器,并接收服务器的回显响应。
# 当用户输入“exit”时,客户端断开连接并退出。

         server:

import socket

def main():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(('localhost', 12345))
    server_socket.listen(5)

    print("Server is listening on port 12345...")

    while True:
        client_socket, client_address = server_socket.accept()
        print(f"Connection from {client_address}")

        data = client_socket.recv(1024)
        while data:
            print(f"Received: {data.decode()}")
            client_socket.sendall(data)
            data = client_socket.recv(1024)

        print("Closing connection")
        client_socket.close()

if __name__ == "__main__":
    main()

# 服务器在本地主机的端口12345上监听连接。
# 当客户端连接到服务器时,服务器会接收客户端发送的数据,并将其回显(发送)回客户端。
# 当客户端断开连接时,服务器关闭与客户端的连接并继续监听新的连接。

pipeline

        Pipeline (Pipe): Pipeline is a byte stream-based communication method, usually used for communication between processes with a parent-child relationship .

        scope: Pipelines are usually used to implement input and output redirection and pipeline operations of command-line programs .

        The following example is implemented in python for os.pipe()communication between parent and child processes.

import os

def main():
    r, w = os.pipe()

    if os.fork():
        # 父进程
        os.close(w)
        r = os.fdopen(r)
        print("Parent: Received message:", r.read())
        r.close()
    else:
        # 子进程
        os.close(r)
        w = os.fdopen(w, 'w')
        w.write("Hello from child process!")
        w.close()

if __name__ == "__main__":
    main()

        More commonly used, commands in Linux such as 

# cat命令读取名为input.txt文件内容,并传递给grep命令过滤包含信息的行,过滤结果保存到名为output.txt的文件中。
cat input.txt | grep 'pattern' > output.txt

There are three processes involved in the example:

  1. catcommand: This process is responsible for reading input.txtthe contents of the file. catThe command outputs the contents of the file to standard output (stdout).

  2. grepCommands: This process is responsible for reading data from standard input (stdin) (i.e. catthe output of commands) and filtering out lines that contain certain patterns. grepThe command outputs the filtered results to standard output.

  3. Output redirection ( >): This action grepredirects the output of the command to output.txta file instead of displaying it on the screen.

anonymous pipe

        Anonymous pipe (Anonymous Pipe): Anonymous pipe is a communication method based on file descriptors , usually used for communication between processes with a parent-child relationship .

        Scope: Anonymous pipes are usually used to implement multi-process parallel computing and data processing tasks.

        Python example showing how to multiprocessing.Pipe()communicate between parent and child processes using:

from multiprocessing import Process, Pipe

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

def main():
    parent_conn, child_conn = Pipe()
    p = Process(target=child, args=(child_conn,))
    p.start()

    print("Parent: Received message:", parent_conn.recv())
    p.join()

if __name__ == "__main__":
    main()

message queue

        Message Queue (Message Queue): Message Queue is a message-based communication method that can be used for communication between processes of any relationship . For example, those that provide more advanced functions and features: RabbitMQ and Celery, etc. 

        Scope: Message queues are usually used to implement asynchronous communication and task distribution in distributed systems and microservice architectures .

        The following example shows how to multiprocessing.Queue()communicate between two processes using:

from multiprocessing import Process, Queue

def producer(queue):
    queue.put("Hello from producer process!")

def consumer(queue):
    print("Consumer: Received message:", queue.get())

def main():
    queue = Queue()
    p1 = Process(target=producer, args=(queue,))
    p2 = Process(target=consumer, args=(queue,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

if __name__ == "__main__":
    main()

Shared memory

        Shared Memory: Shared memory is a memory-based communication method that can be used for communication between processes of any relationship . Shared memory allows multiple processes to access the same memory area, thereby realizing data sharing between processes.
        Scope: Shared memory is often used to implement data exchange and synchronization in high-performance computing and real-time systems .

        The following example shows how to multiprocessing.Value()share memory between two processes using

from multiprocessing import Process, Value

def increment(shared_value):
    shared_value.value += 1

def main():
    shared_value = Value('i', 0)
    print("Initial value:", shared_value.value)

    p = Process(target=increment, args=(shared_value,))
    p.start()
    p.join()

    print("Final value:", shared_value.value)

if __name__ == "__main__":
    main()

Signal

        Signal (Signal): Signal is an event -based communication method for notifying processes about specific events (such as implementing process management (such as starting, stopping, restarting, etc.), error handling (such as segment faults, floating-point exceptions, etc.) and Status notification (such as child process termination, terminal shutdown, etc.) and other functions.
        Scope: Signals are usually used to implement functions such as process management, error handling, and status notification .
        The following Python examples show how to use signalmodules to handle signals

import os
import signal
import time

def signal_handler(signum, frame):
    print(f"Received signal {signum} at {time.time()}")

def main():
    # 注册信号处理函数  
    signal.signal(signal.SIGUSR1, signal_handler)

    # 获取当前进程ID
    pid = os.getpid()
    print(f"Process ID: {pid}")

    # 循环等待信号
    while True:
        print("Waiting for signal...")
        time.sleep(10)

if __name__ == "__main__":
    main()

remote procedure call

        Remote Procedure Call (RPC): RPC is a communication method that allows a function or method to be executed on a remote computer . RPC hides the underlying communication details, making calling remote functions as easy as calling local functions. The following Python example shows how to xmlrpcimplement a simple RPC server and client using the library:
        server:

from xmlrpc.server import SimpleXMLRPCServer

def add(x, y):
    return x + y

def main():
    server = SimpleXMLRPCServer(('localhost', 12345))  # 监听本地主机的端口12345
    server.register_function(add, 'add')  # 服务器注册了一个名为add的函数,用于将两个数相加
    print("RPC server is listening on port 12345...")
    server.serve_forever()

if __name__ == "__main__":
    main()

        client:

import xmlrpc.client

def main():
    proxy = xmlrpc.client.ServerProxy('http://localhost:12345')  # 连接到RPC服务器
    result = proxy.add(1, 2)  # 客户端使用ServerProxy对象调用远程的add函数,并接收返回结果
    print("Result:", result)

if __name__ == "__main__":
    main()

        Scope: RPC is usually used to implement remote service invocation and data exchange in distributed systems and microservice architectures .

Guess you like

Origin blog.csdn.net/lxd_max/article/details/132210449