Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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)

like image 658
goh Avatar asked Aug 04 '11 11:08

goh


1 Answers

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.

like image 185
tsuna Avatar answered Nov 08 '22 19:11

tsuna