473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Polling, Fifos, and Linux

This is my first time working with some of the more lower-level python
"stuff." I was wondering if someone could tell me what I'm doing wrong
with my simple test here?

Basically, what I need is an easy way for application in userspace to
simply echo values "down" to this fifo similar to the way proc files are
used. Is my understanding of fifo's and their capabilities just totally
off base?

I've implemented something very similar in the past in C which works
fine; however, this app immediately takes up 100% of my CPU after the
first read from the fifo, regardless of whatever options I pass to
os.open(). Am I not actually reading the data out of the fifo? Is that
what's causing it to poll infinite thereafter?

Any help would be greatly appreciate, though I'll keep reading and
experimenting in the meantime.

#!/usr/bin/env python

import select
import os

poller = select.poll()
fifo = os.open("fifo", os.O_RDONLY | os.O_NONBLOCK)

poller.register(fifo, select.POLLIN)

while True:
p = poller.poll()
# only have one file
string = os.read(p[0][0], 1)
if len(string):
print string

Jul 21 '05 #1
4 5293
Jeremy Moles wrote:
This is my first time working with some of the more lower-level python
"stuff." I was wondering if someone could tell me what I'm doing wrong
with my simple test here?

Basically, what I need is an easy way for application in userspace to
simply echo values "down" to this fifo similar to the way proc files are
used. Is my understanding of fifo's and their capabilities just totally
off base?


You shouldn't need to use select.poll(), unless I'm missing something.
I was able to get the following to work:

-=-=-

import os

fifo = os.open("fifo", os.O_RDONLY | os.O_NONBLOCK)

while True:
string = os.read(fifo, 1)
if len(string):
print string
# Perhaps add a delay under an else

-=-=-

The Python script, when run, does nothing until you put data into the
fifo from another process. Then it immediately spits the data out,
character by character.

I'm assuming that you've already created the fifo and that it's in the
current working directory.
Jul 21 '05 #2
On Thu, Jul 07, 2005 at 10:21:19PM -0700, Jacob Page wrote:
Jeremy Moles wrote:
This is my first time working with some of the more lower-level python
"stuff." I was wondering if someone could tell me what I'm doing wrong
with my simple test here?

Basically, what I need is an easy way for application in userspace to
simply echo values "down" to this fifo similar to the way proc files are
used. Is my understanding of fifo's and their capabilities just totally
off base?
You shouldn't need to use select.poll(), unless I'm missing something.
I was able to get the following to work:

Ok, you miss something ;) The program you proposed does busy waiting
and without a time.sleep call will consume 100% CPU time :(

Actually, with a named fifo the situation gets even nastier:

import os, select, time

fifo = os.open("fifo", os.O_RDONLY)

while True:
print "SELECT", select.select([fifo],[],[])
string = os.read(fifo, 1)
if len(string):
print string
else:
nf = os.open("fifo", os.O_RDONLY)
os.close(fifo)
fifo = nf
# Perhaps add a delay under an else

The problem is, that select (and poll) show a End-Of-File condition by returning
ready to read. But on a FIFO, when the first client terminates, the reading
end goes into a EOF state till somebody else reopens the fifo for writing.

[This bit of wisdom comes Advanced Programming in the UNIX Environment by
W.R. Stevens p. 400: 'If we encounter the end of file on a descriptor, that
descriptor is considered readbale by select.']

closing the old descriptor must be done after opening a new one, or else you
get a tiny moment where a O_WRONLY client is not able to open the file.
This way there is always a reading client of the fifo.

Andreas


-=-=-

import os

fifo = os.open("fifo", os.O_RDONLY | os.O_NONBLOCK)

while True:
string = os.read(fifo, 1)
if len(string):
print string
# Perhaps add a delay under an else

-=-=-

The Python script, when run, does nothing until you put data into the
fifo from another process. Then it immediately spits the data out,
character by character.

I'm assuming that you've already created the fifo and that it's in the
current working directory.
--
http://mail.python.org/mailman/listinfo/python-list

Jul 21 '05 #3
In article <ma***************************************@python. org>,
Andreas Kostyrka <an*****@kostyrka.org> wrote:
On Thu, Jul 07, 2005 at 10:21:19PM -0700, Jacob Page wrote:
Jeremy Moles wrote:
This is my first time working with some of the more lower-level python
"stuff." I was wondering if someone could tell me what I'm doing wrong
with my simple test here?

Basically, what I need is an easy way for application in userspace to
simply echo values "down" to this fifo similar to the way proc files are
used. Is my understanding of fifo's and their capabilities just totally
off base?
You shouldn't need to use select.poll(), unless I'm missing something.
I was able to get the following to work:

Ok, you miss something ;) The program you proposed does busy waiting
and without a time.sleep call will consume 100% CPU time :(
I don't doubt that it would, but that's because he (like the
original poster) open the file with O_NONBLOCK. From my point
of view that's a self-inflicted injury, but if you start from
the assumption that O_NONBLOCK is needed for some reason, then
the poll makes sense. In normal blocking mode, select+read is
identical to plain read for any kind of file that supports select.
Actually, with a named fifo the situation gets even nastier:

import os, select, time

fifo = os.open("fifo", os.O_RDONLY)

while True:
print "SELECT", select.select([fifo],[],[])
string = os.read(fifo, 1)
if len(string):
print string
else:
nf = os.open("fifo", os.O_RDONLY)
os.close(fifo)
fifo = nf
# Perhaps add a delay under an else

The problem is, that select (and poll) show a End-Of-File condition by
returning
ready to read. But on a FIFO, when the first client terminates, the reading
end goes into a EOF state till somebody else reopens the fifo for writing.

[This bit of wisdom comes Advanced Programming in the UNIX Environment by
W.R. Stevens p. 400: 'If we encounter the end of file on a descriptor, that
descriptor is considered readbale by select.']

closing the old descriptor must be done after opening a new one, or else you
get a tiny moment where a O_WRONLY client is not able to open the file.
This way there is always a reading client of the fifo.


OK, but in more detail, what happens in these two scenarios?
In your version, what happens when the writer opens a pipe
pipe that the reader is about to close? Who reads the data?

On the other hand, if you close the pipe first, what happens
to the writer who happens to try to open the pipe at that moment?

Luckily, as far as I know, we don't have to worry about the
first one, since if data could be lost in this way it would
be much more complicated to close a file descriptor without
running this risk.

But I don't see the second one as much of a problem either.
The writer blocks - so?

Now, what would really be useful is a way for the writer to
detect whether open will block, and potentially time out.

Donn Cave, do**@u.washington.edu
Jul 21 '05 #4
Jeremy Moles wrote:
This is my first time working with some of the more lower-level python
"stuff." I was wondering if someone could tell me what I'm doing wrong
with my simple test here?

Basically, what I need is an easy way for application in userspace to
simply echo values "down" to this fifo similar to the way proc files are
used. Is my understanding of fifo's and their capabilities just totally
off base?


Funny coincidence... I was also trying some FIFO stuff today. I came to a
very simple solution for just one writer and one listener. I make this stuff
send text (strings) from one Konsole to another Konsole.

The FIFO was created from Bash with:

$ mkfifo -m 666 fifo

listener.py
------------------
#! /usr/bin/env python

fifo = open("fifo","r")

while True:
print fifo.readline()[:-1]
# [:-1] because of the newline added by print.
#
------------------

writer.py
------------------
#! /usr/bin/env python

fifo = open("fifo", "a") # Mode "a" or "w"

try:
fifo.write("This string goes down the pipe.\n")
# Newline is important because the listener uses readline().
fifo.flush()
# Output is buffered.
except IOError:
pass
#
------------------

If you kill the writer, the listener remains quiet until somebody writes
into the pipe. The same happens if there is no writer.

If you kill the listener, the writer reports a broken pipe when it tries to
flush().

The writer can close and open the pipe to its liking, the listener doesn't care.

The only problem is that the writer freezes when it opens the pipe until
there is a listener at the other end.

Any way, it's very simple and it uses 0% of the CPU.

Just my 2 Euro cents,

--
==================
Remi Villatel
maxilys_@_tele2.fr
==================
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Daniel Mueller | last post by:
Hello Fellow Python Programmers, I have the following problem: i want to read the content of a file every 10 seconds. now what is the best funktion to do that? to open the file, read it and...
1
by: Sporge | last post by:
Hi All Is there any way to decrease the polling interval that Oracle uses to determine if there are jobs pending in the job queue. It appears that it takes up to 5 seconds for a job to be...
0
by: Vijay Varadan | last post by:
We're using Enterprise Application Block June 2005 w/ Visual Studio 2003 .NET. We've configured our ASP.NET web service to run under a specific domain account using the instructions at...
1
by: Daniel Bass | last post by:
hey guys. overview --------- I'm designing a messaging system that works on the principle of late binding to the I/O objects, depending on the .Net class libraries present in the local folder....
1
by: engwar | last post by:
Is anyone aware of any freely available software written in a .Net language for adding polling features to an existing website? I'm looking for just a polling component and not interested in...
1
by: kelvin.jones | last post by:
Hi guys, I have read several discussions on this group (and others) that talk about polling a server using ajax transactions and if it is possible to push to the client. The general consensus seems...
5
by: Mike | last post by:
Hi everyone, I would like to be able to send updates to my web page from the server. If I understood correctly, this is not possible to achieve in a web environment, unless some sort of polling...
13
by: LordHog | last post by:
Hello all, I have a little application that needs to poll a device (CAN communications) every 10 to 15 ms otherwise the hardware buffer might overflow when there are message burst on the bus. I...
3
by: Chris Mullins [MVP - C#] | last post by:
I'm sitting on the fence on this one, and wanted to get some other people's input. If you're a big B2B person, I would love to hear your feedback... I've got a SOA system. It's based on a...
1
by: johnalexx | last post by:
hi i want to implement a server- client communication in c/c++ 1 server and 2 clients server has two fifo per client one for storing sending data and another for storing recieving data. here we...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.