Python API - How to get data from the uplink_callback method?

Hi,

I’m currently trying to get a copy of the UplinkMessage object from the uplink_callback method to process some values in some kind of endless loop with the Python API.
My code looks something like this:

import time
import ttn
import csv
import os, sys
import queue

#Configuration data
app_id = "------------------------"
access_key = "--------------------"

message_path = os.path.abspath(os.path.dirname(sys.argv[0]))
message_path += '/messages.csv'


last_message = queue.Queue() #initialises a first in first out queue

#Define uplink method
def uplink_callback(msg, client):
    #Create CSV file with message content
    with open (message_path,'a',newline='') as message_file:
        csv_writer=csv.writer(message_file)
        csv_writer.writerow([msg.app_id,msg.dev_id,msg.payload_raw,msg.payload_fields.altitude,msg.payload_fields.hdop,msg.payload_fields.latitude,msg.payload_fields.longitude,msg.metadata.time])
        csv_writer.close()
    global last_message
    last_message.put((msg))




#Create handler for "The Things Network"
handler = ttn.HandlerClient(app_id, access_key, discovery_address="discovery.thethings.network:1900")


#Start mqtt client
mqtt_client = handler.data()
mqtt_client.set_uplink_callback(uplink_callback)
mqtt_client.connect()


while True:
    time.sleep(10)
    if not last_message.empty():
        print(last_message.get())

Despite the “last_message” queue is flagged as global in the uplink_callback method, I wasn’t able to show the data in the endless loop.
Does anyone has an answer to this issue?

Please format your post when including code to keep things readable. Use the pencil icon to edit your previous post to format it afterwards.