0

I'm looking to implement a similar program as tail in my python program.

I can listen to a named pipe via Unix:

tail -f mypipe

This works great (I can echo strings to said pipe), but I'd like to do it in python. I'd like to set the data coming through the pipe to a variable (it's preferable for a var to get updated with the last datum sent over the pipe).

So far I have:

cwd = os.getcwd()
testpath = os.path.join(cwd, 'mypipe')

if os.path.exists(testpath):
    os.remove(testpath)
    mkfifo = os.mkfifo(testpath, 0644)
    rp = open(testpath, 'r')
    response = rp.read()
    print 'response is ', response

There are two issues:

  1. response reads once, but I don't know how I would read the stream upon data being received (something like a listener)
  2. I seem to be running into a blocking issue whereby the pipe is claimed and will not allow other programs to access it. I don't totally understand accessing fifo pipes without blocks and am having a hard time w/ the docs for this.

I've attempted something like what is found here (the highest upvoted answer), but it doesn't seem to work for me. I get an error:

IOError: [Errno 29] Illegal seek

Community
  • 1
  • 1
jml
  • 1,745
  • 6
  • 29
  • 55
  • how are you writing to the pipe? – Padraic Cunningham Aug 22 '15 at 22:07
  • via `echo` inside a shell process – jml Aug 22 '15 at 22:10
  • When I want my Python program to handle input from a pipe, I just use `input()` (or `raw_input()` for Python 2). Will your program do something that doesn't work with that method? – TigerhawkT3 Aug 22 '15 at 22:15
  • I doubt it. As long as I can capture to a variable I'm fine with either. I'll check into those options - in the mean time if you can post this as an answer I would like to see the implementation. – jml Aug 22 '15 at 22:16
  • 1
    I don't see how this is a duplicate. Not only can I not get the code in the other post to work on my machine, but I am asking for an explanation of how I can avoid the subprocess being blocked. This has been unhelpful. – jml Aug 22 '15 at 22:45
  • 1
    In the Windows cmd, `echo 3 | py -c "a = input().strip(); print(int(a)*2 if a.isdigit() else 'not digit')"` will output `6`. If, in your script, you create a loop that will `try:` to `append()` some `input()` to a `list` and `break` on an `EOFError`, you can then print your preferred slice of that `list`. – TigerhawkT3 Aug 22 '15 at 23:11
  • what would the loop check on? the size of the data in the pipe ? – jml Aug 24 '15 at 07:32

0 Answers0