2011-08-04 20 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上で私のために働いている

+0

実際にパイプが作成されますか? –

+0

@トーマス、はいパイプが作成されます。 – goh

答えて

0

をする(Linuxでのpython 2.6.5を実行しています):

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() 

私はこの問題は、あなたがどのプラットフォーム上で、プラットフォームに依存していると思いますか?おそらくいくつかの権限の問題。 man 7 fifoから

+0

私はubuntuを使っています。私は777に権限を変更しましたが、それでも同じ問題です。私は、エラーが "デバイスまたはアドレス"であり、 "ファイルまたはディレクトリ"ではないことが奇妙であることを発見しました – goh

+0

パイプをリモートファイルシステムまたはパイプをサポートしないファイルシステムに作成している可能性があります。 – Ferran

5

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

関連する問題