def run(host, queue, exchange, key):

    # we define a tag based on the queue name
    tag = "%s_tag" % queue

    # set up a connection to the server
    conn = amqp.Connection(host)
    # and get a channel
    chan = conn.channel()

    # define your queue and exchange
    # if they already exist then we check they are of the correct type
    chan.exchange_declare(exchange=exchange, type="direct", 
    durable=False, auto_delete=True)
    # for our purposes queues want to be unique between implementations
    # and exchanges are defined by the paperboy service
    chan.queue_declare(queue=queue, durable=False, 
                       exclusive=False, auto_delete=True)

    # and wire the queue and exchange together
    chan.queue_bind(queue=queue, exchange=exchange, routing_key=key)

    # register our callback function for when we see something on the queue
    chan.basic_consume(queue=queue, no_ack=True, 
                       callback=recv_callback, consumer_tag=tag)

    # set the script to be long running
    try:
        while True:
            chan.wait()
    except KeyboardInterrupt:
        # if we do exit then tell the server
        chan.basic_cancel(tag)

        # and close everything
        chan.close()
        conn.close()

        sys.exit()
Long running message queue processor
dataset/ETHPy150Open garethr/Asteroid/asteroid/bin/asteroid_listen.py/run