goh
goh

Reputation: 29531

os.open(): no such device or address

I want to try my hands on named pipes so I downloaded a piece of code and modified it to test out:

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

I tried running the script, it returns this error:

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'

What is causing this error? (am running python 2.6.5 in linux)

Upvotes: 7

Views: 11599

Answers (2)

tsuna
tsuna

Reputation: 1846

From 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.

So you're getting this confusing error message because you tried to open a named pipe for reading yet, and you're trying to open it for writing with a non-blocking open(2).

In your specific example, if you run child() before parent() then this error would occur.

Upvotes: 13

Ferran
Ferran

Reputation: 15013

This is working for me on 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()

I think the problem is platform dependant, on which platform are you ? o maybe some permissions problem.

Upvotes: 0

Related Questions