473,499 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13803
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5815
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
2
1976
by: Orr, Steve | last post by:
Oracle provides an export utility (exp) and I have a shell script which compresses its output (not stdout) thru a pipe but l need a platform portable Python script for this. Here's the shell...
1
1782
by: Paul Walker | last post by:
Hi all, I'm currently trying to put together a small script to talk to cscope (http://cscope.sf.net/) via pipes. The obvious way to do this is via popen2 (or popen4). I've tried both the one...
6
2500
by: calmar | last post by:
Hi all, I would like to use python for a replacement for some binutils. I would like to be able to pipe things into python. Actually I would not like writing a 'script' to handle the input, but...
2
2332
by: MrEntropy | last post by:
Greetings, I'm having a little trouble getting an idea running. I am writing a C program which is really a frontend to a Python program. Now, my C program starts up, does some initialisation like...
7
1513
by: John Pote | last post by:
Hello, help/advice appreciated. Background: I am writing some web scripts in python to receive small amounts of data from remote sensors and store the data in a file. 50 to 100 bytes every 5 or...
0
264
by: yagyala | last post by:
Hi. I'm trying to communicate with a c++ executable via pipes. I prefer not to use forks because I want it to work on windows. In python the code looks roughly like: (r,w) = pipes()...
1
3105
by: sven _ | last post by:
Keywords: subprocess stdout stderr unbuffered pty tty pexpect flush setvbuf I'm trying to find a solution to <URL:http://bugs.python.org/issue1241>. In short: unless specifically told not to,...
5
705
by: Luis Zarrabeitia | last post by:
I have a problem with this piece of code: ==== import sys for line in sys.stdin: print "You said!", line ==== Namely, it seems that the stdin buffers the input, so there is no reply until ...
0
7128
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7385
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5467
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4917
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4597
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.