How to write a file generator
Thomas Jollans
t at jollybox.de
Tue Jul 12 15:05:52 EDT 2011
On 07/12/2011 06:42 PM, Billy Mays wrote:
> On 07/12/2011 11:52 AM, Thomas Jollans wrote:
>> On 07/12/2011 04:46 PM, Billy Mays wrote:
>>> I want to make a generator that will return lines from the tail of
>>> /var/log/syslog if there are any, but my function is reopening the file
>>> each call:
>>>
>>> def getLines():
>>> with open('/var/log/syslog', 'rb') as f:
>>> while True:
>>> line = f.readline()
>>> if line:
>>> yield line
>>> else:
>>> raise StopIteration
>>>
>>>
>>> I know the problem lies with the StopIteration, but I'm not sure how to
>>> tell the caller that there are no more lines for now.
>>>
>>> --
>>> Bill
>>
>> http://stackoverflow.com/questions/1475950/tail-f-in-python-with-no-time-sleep
>>
>
>
> That was actually the behavior I was trying to avoid. If there is no
> data to be read, the call will hang. That function is actually called
> by a webserver (from wsgiref) so it cannot hang indefinitely.
What Terry said, then. (Not Bruno, I confused that. Sorry for sending a
mail both short and wrong.)
More information about the Python-list
mailing list