Place the pipe in the dictionary

PMU_PIPE_MAP = {}

PIPE= 'tmp/%s.pipe' % hostname
if not os.path.exists(PIPE):
    os.mkfifo(PIPE)

PMU_PIPE_MAP[hostname] = os.open(PIPE, os.O_WRONLY)

      

I am trying to open pipes n

. To keep track of them, I would like to store them somehow - like in a dictionary - I thought the above code should work, but it hangs at runtime. Any suggestions?

This actually works:

pipein = os.open(PIPE, os.O_WRONLY)

      

+3


source to share


2 answers


Aha! apparently there must be a reading on the other end before we can get a return from the pipe. So the question I asked is wrong because I was unable to test both scenarios in the same way. So my problem was understanding how pipes work. In this situation, writing the dictionary will succeed after the pipeline is opened to "read", but will block until then. how to determine if protocol can be written



+1


source


It is untested, but can it help you.

import os, tempfile

tmpdir = tempfile.mkdtemp()

PMU_FIFO_MAP = {}

def openFIFO(pname):
    filename = os.path.join(tmpdir, '%s' % pname)
    fifo = None
    try:
        os.mkfifo(filename)
        fifo = os.open(PIPE, os.O_WRONLY)
    except OSError, e:
        print "Failed to create FIFO: %s" % e
        print e.printStackTrace()
    return fifo

def closeFIFO(fname, fifo):
    fifo.close()
    os.remove(fname)

for hostname in hostnames:
    fifo = openFIFO(hostname)
    if fifo:
        PMU_FIFO_MAP[hostname] = fifo

# do stuff with fifos

for fname, fifo in PMU_FIFO_MAP.items():
    closeFIFO(fname, fifo)
    del PMU_FIFO_MAP[hostname]

os.rmdir(tmpdir)

      



Also see the possible Create temporary FIFO (named pipe) in Python? ...

-2


source







All Articles