python数据管道_Python:检查命名管道是否有数据

I have a Python3 process on my Unix system always running, and I want to be able to randomly send data to it through a named pipe from other processes that only run occasionally. If the named pipe doesn't have data, I want my process to continue doing other things, so I need to check whether it has data without blocking.

I can't figure out how to check without opening it, but opening blocks unless I set the non-blocking flag. And if I set the flag, it crashes if I happen to write to the pipe before or during the read.

This is the best I've managed to do:

import os

fifo = "pipe_test.fifo"

done = False

fd = os.open(fifo, os.O_RDONLY | os.O_NONBLOCK)

while not done:

try:

s = os.read(fd, 1024) # buffer size may need tweaking

print(s)

done = True

except BlockingIOError as e:

pass

os.close(fd)

If there's no data in the pipe, I get b"", and it exits. If there's data in the pipe, it gets an exception once, retries, then gets the data. Seems like I'm doing something wrong and might run into weird race conditions. Is there a nicer way to do this?

解决方案

I would not use a named-pipe if you can change the clients' code, but UNIX domain sockets instead, because they support datagrams:

import errno, fcntl, os, socket

Server:

# bind socket

sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)

sock.bind('pipe_test.fifo')

# set socket non-blocking

fcntl.fcntl(sock.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

# get a datagram

try:

datagram = sock.recv(1024)

except (OSError, socket.error) as ex:

if ex.errno not in (errno.EINTR, errno.EAGAIN):

raise

else:

print('Datagram: %r' % datagram)

Client:

sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)

sock.sendto('Hello!', 'pipe_test.fifo')

But you might want to look into multithreading instead of using non-blocking sockets.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值