2011-08-04 26 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

相關問題