2011-08-04 31 views
3

我想尝试一下我的手,命名管道,所以我下载了一段代码,并修改它来测试:python - os.open():没有这样的设备或地址?

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)

+0

管道实际上是否已创建? –

+0

@Thomas,是管道被创建。 – goh

回答

0

这是为我工作在Linux上:

import time 
import os, sys 
fifoname = '/tmp/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() 

我认为这个问题是与平台相关的,在哪个平台是吗? o也许有一些权限问题。

+0

即时通讯使用Ubuntu。我将权限更改为777,但仍然是同样的问题。我发现奇怪的是,错误说“设备或地址”而不是“文件或目录” – goh

+0

也许你正在远程文件系统或不支持管道的文件系统中创建管道。 – Ferran

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(),则会发生此错误。

+0

这是正确的答案 – klashxx

相关问题