Python手动读写fifo实现进程间异步通信

文章介绍了在Python中如何利用FIFO文件实现进程间的异步通信,特别是在进程已被封装或涉及跨语言通信的场景下。生产者进程不断写入FIFO,而消费者进程使用非阻塞模式读取,避免了因FIFO为空导致的阻塞问题。
摘要由CSDN通过智能技术生成

背景 & 方案适用场景

Python中可以直接使用Multiprocessing包开启多进程,并使用包中的Queue实现进程间的异步通信。

但是如果需要通信的进程已经被封装,或者是其他语言写的程序,又要实现进程间异步通信,可以参考本文的方案:读写FIFO文件。

*注:除本方案外,想实现不同语言的进程进行通信,也可以使用socket通信等方式

实现进程间异步通信

主要用到以下工具包:

# 用Process类管理多进程
import multiprocessing import Process
# 导入系统工具
import fcntl
import os

写FIFO进程

def productor() -> None:
	while True:
		'''
			其他要在进程中实现的逻辑
		'''
		
		try:
			msg = "Hello, World!"
			fifo_fd = os.open("./tmp_pipe", os.O_WRONLY)
			os.write(fifo_fd, msg.encode())
			os.close(fifo_fd)
		except OSError:
			pass

		'''
			其他要在进程中实现的逻辑
		'''
		
		# 防止重复写入fifo过快造成打开失败,如果间隔的逻辑比较多,可以不加入sleep
		time.sleep(1)

读FIFI进程

def customer() -> None:
	while True:
		'''
			其他要在进程中实现的逻辑
		'''
		
		try:
			fifo_fd = os.open("./tmp_pipe", os.O_RDONLY | os.O_NONBLOCK)
			fcntl.fcntl(fifo_fd, fcntl.F_SETFL, os. O_NONBLOCK)
			msg = os.read(fifo_fd, 1024)
			'''
				其他要在进程中实现的逻辑
			'''

			os.close(fifo_fd)
		except OSError:
			pass			
		

主调用进程

def main() -> None:
	if not os.path.exists("./tmp_pipe"):
		os.mkfifo("./tmp_pipe")
	
	p_productor = Process(target=productor)
	p_customer = Process(target=customer)
	p_writer.start()
	p_customer.start()

	timeout = 100
	time.sleep(100)

	p_productor.kill()
	p_customer.kill()
	os.unlink("./tmp_pipe")

问题 & 解决方案

本文中实现了一个生产者消费者模型,在这个情况下会出现一个进程阻塞的问题:fifo空,消费者再读fifo实现后续逻辑时,消费者进程会被阻塞在open fifo一行,所以上述代码使用了如下方式防止阻塞:

fifo_fd = os.open("./tmp_pipe", os.O_RDONLY | os.O_NONBLOCK)
fcntl.fcntl(fifo_fd, fcntl.F_SETFL, os. O_NONBLOCK)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值