gpt4 book ai didi

python - os.open() : no such device or address?

转载 作者:太空狗 更新时间:2023-10-29 22:23:42 49 4
gpt4 key购买 nike

我想尝试命名管道,所以我下载了一段代码并修改了它以进行测试:

fifoname = '/home/foo/pipefifo'                       # must open same name

def child( ):
pipeout = os.open(fifoname, os.O_NONBLOCK|os.O_WRONLY)
# open fifo pipe file as fd
zzz = 0
while 1:
time.sleep(zzz)
os.write(pipeout, 'Spam %03d\n' % zzz)
zzz = (zzz+1) % 5

def parent( ):
pipein = open(fifoname, 'r') # open fifo as stdio object
while 1:
line = pipein.readline( )[:-1] # blocks until data sent
print 'Parent %d got "%s" at %s' % (os.getpid(), line, time.time( ))

if __name__ == '__main__':
if not os.path.exists(fifoname):
os.mkfifo(fifoname) # create a named pipe file
if len(sys.argv) == 1:
parent( ) # run as parent if no args
else:
child()

我尝试运行脚本,它返回了这个错误:

pipeout = os.open(fifoname, os.O_NONBLOCK|os.O_WRONLY)     # open fifo pipe file as fd
OSError: [Errno 6] No such device or address: '/home/carrier24sg/pipefifo'

是什么导致了这个错误? (我在 linux 中运行 python 2.6.5)

最佳答案

来自 man 7 fifo:

A process can open a FIFO in non-blocking mode. In this case, opening for read only will succeed even if no-one has opened on the write side yet, opening for write only will fail with ENXIO (no such device or address) unless the other end has already been opened.

所以你收到这个令人困惑的错误消息是因为你试图打开一个命名管道进行读取,而你正试图用一个非阻塞的open(2)打开它进行写入.

在您的具体示例中,如果您在 parent() 之前运行 child(),则会发生此错误。

关于python - os.open() : no such device or address?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6940293/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com