469,328 Members | 1,247 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

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.

Jul 18 '05 #1
5 13613
Jeroen van der Ham:
f=open('/tmp/myfifo','r')
while 1:
print f.readline()
This loop is endless, so don't complain to us that it doesn't end :-)
what can be done to make it work?


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

--
René Pijlman
Jul 18 '05 #2
Rene Pijlman wrote:
Jeroen van der Ham:
f=open('/tmp/myfifo','r')
while 1:
print f.readline()

This loop is endless, so don't complain to us that it doesn't end :-)

what can be done to make it work?

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


wouldn't this work too?

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

Jul 18 '05 #3
On Sat, 06 Dec 2003 18:42:35 +0100, Rene Pijlman
<re********************@my.address.is.invalid> wrote:

This loop is endless, so don't complain to us that it doesn't end :-)
what can be done to make it work?


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


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.

Jul 18 '05 #4
Jeroen van der Ham:
Rene Pijlman:
f=open('ff','r')
while 1:
li = f.readline()
if not li: break
print li,


This works, but only for a single line.


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.."
http://groups.google.com/groups?thre...va.Helsinki.FI

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

--
René Pijlman
Jul 18 '05 #5
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:
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.


Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Orr, Steve | last post: by
1 post views Thread by Paul Walker | last post: by
reply views Thread by yagyala | last post: by
5 posts views Thread by Luis Zarrabeitia | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.