Reading from a FIFO, burning CPU

Zac Jensen listbox at cybereal.org
Sat Jun 14 23:05:43 EDT 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Saturday 14 June 2003 04:11 pm, Peter Hansen wrote:
> "Elmar W. Hoefner" wrote:
> > Hi there!
> >
> > 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?
> > I did it this way:
> >
> > 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...)
>
> Don't know about FIFOs on Linux, but if it's an entity which returns
> '' when there's nothing else to read for the moment, you will of course
> consume CPU when it has nothing to give you.  Doesn't it block?  Perhaps
> investigating the select.select() function would be a good idea, if it
> works with a fifo...
>
> -Peter

I second that, select() is a much better solution if you're not using win32.  
It won't return unless there is data if you use it correctly, making the 
script even more effective than the previous in the thread about using 
sleep's...

- -Zac
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE+6+KHV3L7YsSif1URAiZ/AJ9F7DFhHi3SIcavfldBExnJqWYmXgCfafrh
uvy8BTlJ8hKmnwJXJ3llI4Q=
=PpSl
-----END PGP SIGNATURE-----






More information about the Python-list mailing list