Multiprocessing multi-process communication pipeline Pipe; the difference between communication interface Queue and pipe

Reference:
https://www.liaoxuefeng.com/wiki/1016959663602400/1017628290184064
https://docs.python.org/3/library/multiprocessing.html

****The difference between multiprocessing Process and Pool creation process interface:
https://blog.csdn.net/Oops_newbie/article/details/80894654

****multiprocessing multi-process, multi-thread case
https://blog.csdn.net/weixin_42357472/article/details/85052929

1. Small knowledge of multiprocessing

****The difference between multiprocessing.Queue and pipe communication interfaces:
https://zhuanlan.zhihu.com/p/24883194?from_voters_page=true

A Pipe() can only have two endpoints.

A Queue() can have multiple producers and consumers.
(1) Queue uses putget to maintain the queue, and pipe uses send recv to maintain the queue.

(2) Pipe only provides two endpoints, while Queue has no restrictions.

This means that when using Pipe, only two processes can be started at the same time. A producer and a consumer operate on these two endpoints (the two values ​​returned by pipe()), which together maintain a queue. If multiple processes operate on the same endpoint of the pipe at the same time, errors will occur (since there are no locks, similar to thread-unsafety). Therefore, two endpoints are equivalent to providing only two safe operation locations for the process, thus limiting the number of processes to only 2.

(3) The encapsulation of Queue is better, Queue only provides one result, which can be called by multiple processes at the same time; Pipe() returns two results, which are called by two processes respectively.

(4) The implementation of Queue is based on pipe, so the running speed of pipe is much faster than Queue

(5) When only two processes are required, the pipeline is faster, and when multiple processes are required to operate the queue at the same time, the queue is used.
insert image description here

2. The main process of the Pipe pipeline needs to communicate with the child process

1) Create a child process

# 在主进程创建管道
parentPipe2, childPipe2 = Pipe(True)
# 在主进程中创建子进程
proc2 = Process(target=run_asr_realtime, args=(childPipe2, ''))

# 启动子进程
proc2.start()

2) The main process needs to communicate with the child process through the created pipeline for information communication

parentPipe2, childPipe2 = Pipe(True)

#childPipe2在子进程逻辑中往管道send内容

childPipe2.send(text)

#parentPipe2在主进程中从管道中recv内容
parentPipe2.recv()
from multiprocessing import Process, Pipe
import time

def run_asr_realtime(pipe, text):
    # 模拟实时语音转录逻辑
    # 这里只是一个示例,你需要根据实际情况替换为你的语音转录代码
    for i in range(5):
        time.sleep(1)
        text = f'转录的文本 {i+1}'
        pipe.send(text)

# 在主进程创建管道
parentPipe2, childPipe2 = Pipe(True)

# 在主进程中创建子进程
proc2 = Process(target=run_asr_realtime, args=(childPipe2, ''))

# 启动子进程
proc2.start()

# 在主进程的循环中不断获取转录的文本
while True:
    if parentPipe2.poll():
        text = parentPipe2.recv()
        print(f'从子进程接收到的文本: {text}')
    else:
        # 执行其他操作
        time.sleep(0.1)

    # 终止主进程的循环条件
    if text == '终止条件':
        break

# 等待子进程结束
proc2.join()

Guess you like

Origin blog.csdn.net/weixin_42357472/article/details/131473458