turning callback into generator

Bengt Richter bokr at oz.net
Mon Sep 6 20:06:59 EDT 2004


On Mon, 06 Sep 2004 23:24:17 +0200, Peter Otten <__peter__ at web.de> wrote:

>Wai Yip Tung wrote:
>
>> I'm attempting to turn some process than uses callback to return result
>> into a more user friendly generator. I'm hitting some road block so any
>> pointer would be appriciated.
>
>I asked the same question a while back and there were no satisfying
>suggestions.
>
>http://mail.python.org/pipermail/python-list/2003-December/197726.html
>
>At the very least you need threads, and when the generator is not fully
>exhausted it's easy to end up with a thread waiting forever. 
>
>I finally dropped the idea, but if you are really determined and want to
>hack something together, the following might (or might not, this is really
>a shot in the dark) serve as a starting point:
>
>http://mail.python.org/pipermail/python-list/2003-July/173872.html
>

Not that familiar with python threads and queue, but here's a start:

 >>> import threading
 >>> import Queue
 >>> q = Queue.Queue(3) # ridiculously short
 >>>
 >>> def producer(n, cb):
 ...     for i in xrange(n): cb(i)
 ...
 >>> def product_generator(p, *a, **kw):
 ...     tpq = threading.Thread(target=p, args=a, kwargs=kw)
 ...     tpq.start()
 ...     try:
 ...         while True: yield q.get(True,5)
 ...     except Queue.Empty:
 ...         print 'No data for 5 seconds'
 ...
 >>> for item in product_generator(producer, 8, q.put): print item
 ...
 0
 1
 2
 3
 4
 5
 6
 7
 No data for 5 seconds
 >>>

Not tested beyond what you see ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list