def generate_in_background(generator, num_cached=10):
 import Queue
 queue = Queue.Queue(maxsize=num_cached)
 sentinel = object()
 def producer():
  for item in generator:
   queue.put(item)
  queue.put(sentinel)
 import threading
 thread = threading.Thread(target=producer)
 thread.daemon = True
 thread.start()
 item = queue.get()
 while (item is not sentinel):
  (yield item)
  item = queue.get()

def generate_in_background ( generator , num_cached = 10 ) : import Queue queue = Queue . Queue ( maxsize = num_cached ) sentinel = object ( ) def producer ( ) : for item in generator : queue . put ( item ) queue . put ( sentinel ) import threading thread = threading . Thread ( target = producer ) thread . daemon = True thread . start ( ) item = queue . get ( ) while ( item is not sentinel ) : ( yield item ) item = queue . get ( )
runs a generator in a background thread .