Turning f(callback) into a generator

Peter Otten __peter__ at web.de
Thu Dec 4 04:51:07 EST 2003


Diez B. Roggisch wrote:

>> This is a strange asymmetry, and I was wondering if I've overlooked a
>> simple way to transform a callback into a generator, but I now tend to
>> the assumption that you *need* threads: One thread with the callback puts
>> names or name lists into the queue until it's full (some arbitrary limit
>> that also determines the total memory needed), another thread (the
>> generator) consumes names from the queue.
> 
> I also thought about that - its the only thing that allows for real
> lazyness - you can wait in the callback until the generator clears a
> semaphore. But if the context-changes are worth the effort is
> questionable.

There is another aspect I forgot to mention. Your approach would only start
to yield results after all results are found, i. e. in the example you
would have to scan the whole harddisk even when the first visited file
might have been the one you want.
So I think it is best to either not change the callback approach or use
threads as outlined by Jimmy Retzlaff.

Peter




More information about the Python-list mailing list