0

I need to communicate with a process (Kamailio SIP server) using named FIFOs in /tmp. The way it works is that I need to set up a response FIFO of my own and then write the request to Kamailio which will write the response back to my FIFO.

So, how I understand it should go..

  • mkfifo a new temporary FIFO in /tmp for receiving the response. I use os.getpid() to make a unique name such as /tmp/response_1234_fifo
  • try to open(fifo, "r") the FIFO and read() it.. but this will block in open() since no-one is yet opening the FIFO for write

  • write the request to the well known static FIFO (for example /tmp/request_fifo). This request includes the name of my response FIFO

  • Kamailio will process the request, open my response FIFO for writing and write() the response to it.

  • At this point my open() should probably unblock and the read() receive the data. But it doesn't seem to do so. It works sometimes but sometimes doesn't.

  • After writing the response, Kamailio will close my response FIFO which would result in an EOF for the next read()

  • Now I can rm the temporary response FIFO

My brain has shut down after trying to do this in several different ways including threading, popen:ing a "cat < response_fifo" and even trying to use nonblocking open and polling..

If I look at how the Kamailio shell script for running commands does it, it's so simple it's making me cry :)

    cat < $response_fifo &
    printf "$CMD" > $request_fifo
    wait
    rm $response_fifo

Anyone have an elegant solution in Python?

vaizki
  • 1,678
  • 1
  • 9
  • 12
  • 1
    Spawning a thread that writes to the static FIFO while the main thread does a blocking read on the response FIFO looks like the obvious way to do this. That corresponds quite directly to the `cat &` in the shell script. – Fred Foo Nov 08 '13 at 15:30
  • 1
    You can also use `select.select` to see wether you can read and write within one thread. – User Nov 08 '13 at 20:37
  • larsmans, to duplicate the script, I actually did a separate thread that open()+read()s the response FIFO and then write() in the main thread to the request FIFO + join() the reader thread and then get the result from a class variable there. It works but not always. I will have to strace this in more detail to understand what is going on. – vaizki Nov 12 '13 at 07:47

0 Answers0