[PyAR2] Loop? Sockets?

Jeff Rush jeff at taupro.com
Mon Feb 18 00:53:22 CET 2008

W W wrote:
> I have a question about efficiency I suppose it is.
> I'm experimenting with a chat type program, and here's question:
> Would it be considered efficient to run an infinite loop continually 
> opening and reading a file, say
> while True:
>    f = open('convo.txt', 'r')
>    f = f.readlines()
>    print f
> Or some variant of that? Or are there sockets(?) or something that would 
> work better? Basically I just want to be able to open it whenever the 
> file is changed, so that seems like overkill (and probably a waste of 
> cycles).

Hi W,

There are several problems with this approach.

1. Will consume lots of CPU.
2. Will run out of open file descriptors since you are re-opening
3. Will get partial data, in that when you re-read the file, the
    other writer may not be finished yet

Use of pyinotify to detect when a file is changed will alleviate #1, and 
closing after each read will eliminate #2 but you'll need to add special 
handling of incomplete lines to tackle #3.

Sockets really are the correct approach to take on something like this, but 
you'll need to move to an event-driven arrangement to get most of the 
benefits.  The Twisted framework for Python can do chats like this in about 10 
lines of Python source, or if you'd rather learn the low-level details I'd 
read up about sockets, both Unix/local sockets (talking within a single 
machine like you are now with a file) and Network sockets (between machines).


More information about the PyAR2 mailing list