Reading from a FIFO, burning CPU

Donn Cave donn at drizzle.com
Sun Jun 15 00:24:28 EDT 2003


Quoth "Elmar W. Hoefner" <elmar.hoefner at uibk.ac.at>:
| I tried to continuously stream a FIFO into my python script.
| I was successful so far, but the performance is not acceptable: If I start
| the script, it eats 99% of my CPU cycles. Can anyone give me a hint how to
| change this?

| fifo=open("/test_fifo", "r")
| while 1:
|         line=fifo.readline()
|         # to stop the repeated printing of " '' " while nothing is in the fifo:
|         if line != '': print line 
|
| Anything better? I'm sure there is, any help is most appreciated! (Google
| didn't help me, nor did the archives of this group...)

I reckon the other process wrote to the fifo, and then closed it.
In the somewhat obnoxious logic of fifos, that means that you should
see an end of file, and you do -- that's what end of file looks like.

You may close the fifo, and open it again and resume reading.  Your
reads will "block" until data is ready.  You only need to re-open the
fifo when you encounter end of file (zero length string.)

	Donn Cave, donn at drizzle.com





More information about the Python-list mailing list