Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9325

Re: How to write a file generator

From Billy Mays <noway@nohow.com>
Newsgroups comp.lang.python
Subject Re: How to write a file generator
Date 2011-07-12 12:42 -0400
Organization Aioe.org NNTP Server
Message-ID <ivhthn$peg$1@speranza.aioe.org> (permalink)
References <ivhmoc$6t3$1@speranza.aioe.org> <mailman.944.1310485982.1164.python-list@python.org>

Show all headers | View raw


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.

--
Bill

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to write a file generator Billy Mays <noway@nohow.com> - 2011-07-12 10:46 -0400
  Re: How to write a file generator "bruno.desthuilliers@gmail.com" <bruno.desthuilliers@gmail.com> - 2011-07-12 08:34 -0700
  Re: How to write a file generator Thomas Jollans <t@jollybox.de> - 2011-07-12 17:52 +0200
    Re: How to write a file generator Billy Mays <noway@nohow.com> - 2011-07-12 12:42 -0400
      Re: How to write a file generator Thomas Jollans <t@jollybox.de> - 2011-07-12 21:04 +0200
      Re: How to write a file generator Thomas Jollans <t@jollybox.de> - 2011-07-12 21:05 +0200
  Re: How to write a file generator sturlamolden <sturlamolden@yahoo.no> - 2011-07-12 09:53 -0700
  Re: How to write a file generator Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-07-13 01:26 +0200

csiph-web