473,691 Members | 3,091 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 13815
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.add ress.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
5846
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 $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child
2
1985
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 script: ------------------------------------------------------------------ #!/bin/bsh LOG_NAME='/tmp/expzip.log' FILENAME='/tmp/expzip.dmp' /bin/mknod $FILENAME p
1
1787
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 in the os module and the one in the popen2 module, and I get the same effect with both - I don't get any response on the stdout pipe until I've closed the stdin pipe, even if I call flush().
6
2516
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 write the python commands right onto the command line. It would be possible to use here-documents (for having the correct identation during e.g while loops) for writing python code on the fly, but then how and where to get the pipe output:
2
2342
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 initialisation of it's variables and Py_Initialize() and then it fork()s. After forking the child launches the python program with Py_Main() and with the parent I want to be able to read the variables of the Python program. I have tried many ways...
7
1520
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 10 minutes. A new file for each day is anticipated. Of considerable importance is the long term availability of this data and it's gathering and storage without gaps. As the remote sensors have little on board storage it is important that a
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() spawnl(P_WAIT, 'tool.exe', 'dummy', message, str(w)) close(w) print os.read(r, 1000) close r
1
3123
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, normal C stdio will use full output buffering when connected to a pipe. It will use default (typically unbuffered) output when connected to a tty/pty. This is why subprocess.Popen() won't work with the following program when stdout and stderr are...
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 a huge amount of text has bin written. The iterator returned by xreadlines
0
8598
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9077
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8793
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6457
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4322
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2965
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 we have to send another system
3
1952
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.