← Back to team overview

txamqp-user team mailing list archive

Re: How do you listen to different queues asynchronously?

 

Hi Ale

On 11/16/09, Ale <peralta.alejandro@xxxxxxxxx> wrote:
>> Yeah :-), I was thinking more of breaking the consumer loop because
>> I'm quiting my program and I want to end cleanly. Maybe using a global
>> boolean variable? Sending a STOP message from the consumer to
>> terminate that same process doesn't look right.

IMHO, a cleaner approach is to do:

STOP_TOKEN = object()

@inlineCallbacks
def consumer_fanout(conn, chan, queue):

    while True:
        msg = yield queue.get()
        if msg is STOP_TOKEN:
            queue.close()
            break

        print '[FANOUT] Received: ' + msg.content.body + ' from
channel #' + str(chan.id)

    yield chan.basic_cancel("testtag_fanout")

and then, when the user quits the application, put the STOP_TOKEN
object in the queue:

    queue.put(STOP_TOKEN)

remember that when you call queue() on a channel instance, you get an
instance of DeferredQueue
(http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.defer.DeferredQueue.html),
so you're free to put whatever you want in it without having to send
anything through the network :-)

Cheers.



Follow ups

References