Implements interfaces: twisted.internet.interfaces.IConsumer

I act like an IConsumer for your Twisted code and an iterator for your blocking code running via a ThreadWorker.

This is handy when you are using a conventional library that relies on an iterator as its input:

 def render(request):
     w = png.Writer()
     c = asynqueue.Consumerator()
     c.deferUntilDone().addCallback(lambda _: request.finish())
     p = self.producePixelRows(c)
     w.write(request, c)
     return server.NOT_DONE_YET

I work with either an IPushProducer or an IPullProducer. You can construct me with an instance of the former and I'll get started right away. Otherwise, call my registerProducer method with the producer and whether it is streaming (push) or not.

ParametersproducerThe producer for my instance to register, if you want to supply an IPushProducer one on instantiation. Otherwise, use registerProducer.
Instance Variable runState 'init', 'started', 'running', 'stopping', 'stopped'
Instance Variable d A Deferred that fires when iterations are done.
Method __init__ Consumerator(producer=None, maxThreads=None)
Method loop Runs a loop in a dedicated thread that waits for new iterations to be produced.
Method stop Good manners urge you to call this to cleanly break out of a loop of my iterations so that my producer doesn't keep working for nothing.
Method write The producer calls this with a chunk of data. It goes through two stages to emerge from my blocking end as an iteration, via next.
Method registerProducer IConsumer implementation
Method unregisterProducer IConsumer implementation

Inherited from PoolUser (via IterationGetter):

Class Method setup Sets up stuff class-wide, with all the potential pitfalls that entails.
Class Method shutdown Shuts down all threads, returning a Deferred that fires when everything's done, class-wide.
Class Method deferToThreadInPool Runs the f-args-kw call combo in one of my threads, returning a Deferred that fires with the eventual result. Can be run from the class or any instance of me with the exact same result.
Method pool Returns a reference to the class-wide threadpool, starting it if this is the first time it's been used.

Inherited from PoolUser (via IterationGetter):

Class Method setup Sets up stuff class-wide, with all the potential pitfalls that entails.
Class Method shutdown Shuts down all threads, returning a Deferred that fires when everything's done, class-wide.
Class Method deferToThreadInPool Runs the f-args-kw call combo in one of my threads, returning a Deferred that fires with the eventual result. Can be run from the class or any instance of me with the exact same result.
Method pool Returns a reference to the class-wide threadpool, starting it if this is the first time it's been used.
runState =
'init', 'started', 'running', 'stopping', 'stopped'
d =
A Deferred that fires when iterations are done.
def __init__(self, producer=None, maxThreads=None):

Consumerator(producer=None, maxThreads=None)

def loop(self):

Runs a loop in a dedicated thread that waits for new iterations to be produced.

When I get an instance of IterationGetter.IterationStopper, the loop exits. I then call my "all done" Deferred and delete my reference to the producer.

def stop(self):

Good manners urge you to call this to cleanly break out of a loop of my iterations so that my producer doesn't keep working for nothing.

Calling this method at the Twisted main-loop level is also a fine way to quit producing and iterating when you know you're done.

Not part of the official iterator implementation, but useful for a Twisted way of iterating. You need a way of letting whatever is producing the iterations know that there won't be any more of them.

def write(self, data):

The producer calls this with a chunk of data. It goes through two stages to emerge from my blocking end as an iteration, via next.

def registerProducer(self, producer, streaming):

IConsumer implementation

def unregisterProducer(self):

IConsumer implementation

API Documentation for AsynQueue, generated by pydoctor at 2022-11-17 13:13:24.