管道处理中Transformer进程接收参数是否合规?
问题:转换进程的参数设计是否正确?
现有代码包含两个进程逻辑:
- 源进程:从元组读取元素,逐个写入匿名管道
- 转换进程:读取匿名管道中的内容,将消息按空格分词后写入命名管道(FIFO)
疑问:将received_messages作为转换进程的参数是否正确?还是转换进程不应设置任何参数?
以下是编写的代码:
def source_process(): received_messages = [] p = os.pipe() #p[0] is the reader and p[1] is the writer in the returned tuple of os.pipe() if not p: print ("pipe call!") os.close(p[0]) os.close(p[1]) os._exit(1) pid = os.fork() if pid == -1: print (os.strerror("fork call")) os.close(p) os._exit(1) elif pid == 0: #child process os.close (p[0]) # child does not read because here we close p[0] os.write (p[1], MESSAGES[0].encode()) # encode for converting strings to bytes os.write (p[1], MESSAGES[1].encode()) os.write (p[1], MESSAGES[2].encode()) os.write (p[1], MESSAGES[3].encode()) os.close(p[1]) else: # if the code reaches this line pid!=0 and thus we are in the parent process os.close(p[1]) # parent does not write because here we close p[1] for message in MESSAGES: read_message = os.read(p[0], len(message)); received_messages.append(read_message.decode()) os.wait() os.close(p[0]) return received_messages def transformer_process(received_messages): messages_splited=[] for received_message in received_messages: word=received_message.strip().split() messages_splited.append(word) try: # Open the named pipe for writing with open(fifoname, 'w') as pipeout: for message_splited in messages_splited: # Write each tokenized word separately to the named pipe for word in message_splited: pipeout.write(word + '\n') except Exception as e: print(f"Error in child process: {e}") finally: os._exit(0) return messages_splited
结论
这种写法不正确,需要修改:
- 按照设计逻辑,转换进程应该直接从匿名管道读取内容,而非接收
received_messages参数——当前写法跳过了进程间管道通信的核心步骤,直接传递内存中的列表,违背了原本的进程间通信设计。 - 另外,当前代码里
transformer_process并未作为独立进程启动(缺少fork调用),即便要让它成为独立进程,也不能直接通过参数传递Python列表对象,fork后的子进程虽会复制父进程内存,但这不符合管道通信的设计初衷。
内容的提问来源于stack exchange,提问作者athant4
相关产品推荐
相关产品推荐

