On Mon, 2006-03-20 at 07:44 -0800, Ido Abramovich wrote:
> I want to build a generator that can update it's content during
> runtime, but I'm not sure whether what I'm doing works by mistake or
> works by purpose.
>
> I've read PEP-255 about Generators, but nothing is mentioned about
> changing the inner space of the generator during runtime - this could
> mean that "it works by mistake" but from what I understand about
> generators - this behavior is a by-product of the algorithm.
>
> I'm attaching an example of a generator and an iterator that are
> supposed to do produce the same output. The Iterator doesn't work and
> produces a RuntimeError while the Generator finishes the task
> successfully.
>
> What do you think? is it safe to use the generator function? if not -
> do you have another safe approach to add items to a generator during
> runtime?
An approach, which worked for me (in Python 2.3) is to use a List:
- One process (loosely defined here as an invocation of a callback
from a framework with a mainloop, such as Tk) adds items to the
List.
- The generator is invoked by another callback and removes items
from the List.
The problem, which I had, was that I did not see an obvious way to yield
control back to Tk (rather than to the generator's caller) whenever the
List is empty. So I designed the generator to return None when the List
is empty; and have the calling code (a callback function - see above)
return control to Tk in this case.
Does anyone know a better pattern for this?
I use Python 2.3, but can switch to Python 2.4 if necessary.
--- Omer
--
Every good master plan involves building a time machine. Moshe Zadka
My own blog is at http://tddpirate.livejournal.com/
My opinions, as expressed in this E-mail message, are mine alone.
They do not represent the official policy of any organization with which
I may be affiliated in any way.
WARNING TO SPAMMERS: at http://www.zak.co.il/spamwarning.html