def EnqueueThread(it, q=None, name='enqueue', start=True, enqueue=enqueue, **kwargs): """Starts a thread which enqueues the values of the iterable `it`. If a queue is not passed, a new one is created. A reference to the queue is stored as the property `q` of the returned thread. The thread will be started unless `start` is false. This is ideal for processing generators and other iterables which lazily process I/O-bound operations. Passing the return value of a `dequeue` call will have the effect of creating a processing thread which pulls from and pushes to thread-safe data structures. Additional keyword arguments are passed on to `enqueue`. """ from threading import Thread if q is None: q = CloseableQueue() thread = Thread(name=name, target=enqueue, args=(it, q), kwargs=kwargs) thread.q = q if start: thread.start() return thread