Search code examples
python-2.7functioncallbackglobal-variablespython-pika

Why isnt this global variable being updated in python 2.7 even tho I am using the global keyword?


I have a callback function which returns the body of a AMQP message as each message is received. I would like to be able to assign the content of the body to a global variable and then work with it further on in my program.

I have created a global variable at the top of my script and have used the global keyword within the callback function which I understand should allow me to update the global variable at the top of my script, however when I import the 'global variable' from another script I get the original value of 'test' returned rather than the current value of body from within the callback function...

Consumer.py

**python/pika imports and credentials go here**

T_MSG = "test" # create global variable

def main():
    logging.basicConfig()

    (username, password) = open(CREDENTIALS_FILE).read().splitlines()
    credentials = pika.PlainCredentials(username, password)
    params = pika.ConnectionParameters(AMQP_HOST, AMQP_PORT, AMQP_EXCHANGE, credentials)
    connection = pika.BlockingConnection(params)

    channel = connection.channel()

    channel.queue_declare(queue=QUEUE_NAME, durable=False)

    def callback(ch, method, properties, body):
        #print "received message: %r" % body

        global T_MSG # Define that I want to work with the global variable

        #assert isinstance(body, object)

        T_MSG = str(body) # assign the value of body to the global variable 

        print T_MSG # this prints out the body as expected

        return T_MSG
        #ch.basic_ack(delivery_tag = method.delivery_tag)

    #channel.basic_qos(prefetch_count=1)

    channel.basic_consume(callback, queue=QUEUE_NAME, no_ack=False)
    print 'waiting for new messages on queue: '
    channel.start_consuming()

Importer.py

from consumer import T_MSG

print T_MSG # this prints the original value of T_MSG e.g. 'Test'

I cant understand why the value of T_MSG is not being updated from within the callback function as I can clearly see that the print statement is printing the correct value of body - but even though I have declared this as global within the callback function, when I import the value of T_MSG and try to print it, I get the originally assigned value of 'Test' rather than the content of the body I am expecting...

I am under the impression that each time the callback function receives a message and 'processes' it, the new value of body should be assigned to the T_MSG (global) variable and thus I should be able to import it into my importer.py script - but this isn't working and everything I have read just points to using the global keyword within the function to make this so...

Any help is much appreciated!


Solution

  • After importing, you need to call consumer.main() to get your variable changed:

    import consumer
    print consumer.T_MSG  # here you should see the original value
    consumer.main()
    print consumer.T_MSG  # now it will be updated