Reading pipes in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeroen van der Ham

    Reading pipes in Python

    Hello,

    During my exploration of Python and rewriting something from Perl to
    Python I ran into something odd. I want to use a pipe so that a log of
    a program goes into a Python script instead of a log file.

    In Perl you just open a file, but specify that it is actually a pipe,
    but you read from it the same way.
    So I thought I'd do that with the following Python code:

    f=open('/tmp/myfifo','r')
    while 1:
    print f.readline()

    Now, here's the weird thing:
    - On Darwin this seems to work okay (seen it work odd on netmounts
    tho, but not able to test that atm)
    - On Linux this works fine until you send something to the pipe, after
    which it prints it and starts printing blank lines over and over.

    Even when using os.mkfifo() to create the pipe, this still does not
    work properly on Linux.
    Anyone any idea why this isn't working and what can be done to make it
    work?

    Regards, Jeroen.

  • Rene Pijlman

    #2
    Re: Reading pipes in Python

    Jeroen van der Ham:[color=blue]
    >f=open('/tmp/myfifo','r')
    >while 1:
    > print f.readline()[/color]

    This loop is endless, so don't complain to us that it doesn't end :-)
    [color=blue]
    >what can be done to make it work?[/color]

    f=open('ff','r' )
    while 1:
    li = f.readline()
    if not li: break
    print li,

    --
    René Pijlman

    Comment

    • Bryan

      #3
      Re: Reading pipes in Python

      Rene Pijlman wrote:
      [color=blue]
      > Jeroen van der Ham:
      >[color=green]
      >>f=open('/tmp/myfifo','r')
      >>while 1:
      >> print f.readline()[/color]
      >
      >
      > This loop is endless, so don't complain to us that it doesn't end :-)
      >
      >[color=green]
      >>what can be done to make it work?[/color]
      >
      >
      > f=open('ff','r' )
      > while 1:
      > li = f.readline()
      > if not li: break
      > print li,
      >[/color]

      wouldn't this work too?

      for line in file('/tmp/myfifo'):
      print line

      Comment

      • Jeroen van der Ham

        #4
        Re: Reading pipes in Python

        On Sat, 06 Dec 2003 18:42:35 +0100, Rene Pijlman
        <reply.in.the.n ewsgroup@my.add ress.is.invalid > wrote:[color=blue]
        >
        >This loop is endless, so don't complain to us that it doesn't end :-)
        >[color=green]
        >>what can be done to make it work?[/color]
        >
        >f=open('ff','r ')
        >while 1:
        > li = f.readline()
        > if not li: break
        > print li,[/color]

        This works, but only for a single line.

        The idea is that the pipe takes the place of a log of an application
        so that the script can do something because something is written to
        the log.

        Jeroen.

        Comment

        • Rene Pijlman

          #5
          Re: Reading pipes in Python

          Jeroen van der Ham:[color=blue]
          >Rene Pijlman:[color=green]
          >>f=open('ff',' r')
          >>while 1:
          >> li = f.readline()
          >> if not li: break
          >> print li,[/color]
          >
          >This works, but only for a single line.[/color]

          No, it works for multiple lines as well.

          What you probably mean is: it works only for a single writer process. This
          is what Linus said about it in 1993: "That's how a fifo is supposed to
          work as far as I can tell: when all writers have exited, the reader gets
          an EOF and also exits.."


          Try 'cat' instead of a Python script and you'll see exactly the same
          behaviour.

          --
          René Pijlman

          Comment

          • Steve

            #6
            Re: Reading pipes in Python

            How do we open a pipe programatically ? I'm interested in redirecting
            output from a program to a pipe and then using it from a python script.
            Thanks

            Steve

            Jeroen van der Ham wrote:[color=blue]
            > Hello,
            >
            > During my exploration of Python and rewriting something from Perl to
            > Python I ran into something odd. I want to use a pipe so that a log of
            > a program goes into a Python script instead of a log file.
            >
            > In Perl you just open a file, but specify that it is actually a pipe,
            > but you read from it the same way.
            > So I thought I'd do that with the following Python code:
            >
            > f=open('/tmp/myfifo','r')
            > while 1:
            > print f.readline()
            >
            > Now, here's the weird thing:
            > - On Darwin this seems to work okay (seen it work odd on netmounts
            > tho, but not able to test that atm)
            > - On Linux this works fine until you send something to the pipe, after
            > which it prints it and starts printing blank lines over and over.
            >
            > Even when using os.mkfifo() to create the pipe, this still does not
            > work properly on Linux.
            > Anyone any idea why this isn't working and what can be done to make it
            > work?
            >
            > Regards, Jeroen.
            >[/color]

            Comment

            Working...