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

Im trying to open n pipes. In order to keep track of them I'd like to store them somehow - like in a dictionary - I thought that the above code should work but it freezes during execution. Any suggestions?

This does work however:

pipein = os.open(PIPE, os.O_WRONLY)
channon
  • 362
  • 3
  • 16
  • when ^C - ing the line being executed is PMU_PIPE_MAP[hostname] ... – channon Jul 23 '15 at 16:44
  • whats weird is that if I take out the last line then it doesn't freeze and if i look in ./tmp I see all the hostname.pipe files. I am running the program with sudo also and I am using python2.7 although the receiving side is running python 3.4 – channon Jul 23 '15 at 16:58
  • Aha! apparently there has to be a read on the other end before we can get a return from the pipe. So the question that I asked was not correct because I failed to test the two scenarios in the same way. So my problem was in understanding how pipes work. In this situation the dictionary entry will succeed once the piping is opened on the 'read' end but will block until then. – channon Jul 23 '15 at 17:16

2 Answers2

1

Aha! apparently there has to be a read on the other end before we can get a return from the pipe. So the question that I asked was not correct because I failed to test the two scenarios in the same way. So my problem was in understanding how pipes work. In this situation the dictionary entry will succeed once the piping is opened on the 'read' end but will block until then. how to determine if pipe can be written

Community
  • 1
  • 1
channon
  • 362
  • 3
  • 16
-2

It is not tested, but may it will 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 possible Create a temporary FIFO (named pipe) in Python?.

Community
  • 1
  • 1
wenzul
  • 3,948
  • 2
  • 21
  • 33
  • You give no explanation of what was wrong with the OP code. You don't explain why all this extra complexity will help. And your `except OSError` is exactly how exceptions shouldn't be handled (you substitute a single line message for a potentially useful backtrace; your try/else block is not a substitute for a failed mkfifo. It is best to test code before posting; otherwise you can make a confused OP even more confused. – msw Jul 23 '15 at 16:50
  • Thanks for trying to help! The problem was the blocking mechanisms, not the control flow of the writing end. – channon Jul 23 '15 at 17:18
  • @msw I rearanged the try/except block. Could you figure out (may pastebin) how should I do it right? Thanks. – wenzul Jul 24 '15 at 08:50