Python 订阅AWS Greengrass时,如何使用回调函数中接收的变量?

Python 订阅AWS Greengrass时,如何使用回调函数中接收的变量?,python,callback,aws-iot-greengrass,Python,Callback,Aws Iot Greengrass,我有一个单独的应用程序正在向AWS Greengrass发送一些坐标。 我想用这段代码做的是得到这些坐标并使用它们 为了订阅绿草,我使用AWSIoTPythonSDK.MQTTLib 本质上,我订阅了Greengrass上的一个主题,并且在回调函数中接收/处理消息 我想澄清一下,我能够订阅并获取回调函数中的坐标。我没有做到的是在代码中进一步使用这些值 我的代码的简短版本如下所示: coordinateX=0 coordinateY=0 def callbackFunction(): #

我有一个单独的应用程序正在向AWS Greengrass发送一些坐标。 我想用这段代码做的是得到这些坐标并使用它们

为了订阅绿草,我使用AWSIoTPythonSDK.MQTTLib

本质上,我订阅了Greengrass上的一个主题,并且在回调函数中接收/处理消息

我想澄清一下,我能够订阅并获取回调函数中的坐标。我没有做到的是在代码中进一步使用这些值

我的代码的简短版本如下所示:

coordinateX=0
coordinateY=0

def callbackFunction():
    #unpacks the payload and extracts the coordinates
    coordinateX=unpackedCoordinateX
    coordinateY=unpackedCoordinateY
class configureAWS:
    #holds the necessary data in order to connect to AWS

def connectAws():
    #connects to AWS

main:
    connectAws()
    myAWSIoTMQTTClient.subscribe()
    while(True):
        #I want to do something with the variables coordinateX and coordinateY but they are always 0
以下是实际代码:

import numpy as np
import cv2
import time 
import os
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import json


coordinateX=0
coordinateY=0

def customCallback(client, userdata, message):
    payload=json.loads(message.payload)
    #payload is a string representing a json in the form of 'rover':'(x,y)'
    coordinates=list(payload['rover'].split(','))

    coordinateX=int(coordinates[0].replace('(',''))
    coordinateY=int(coordinates[1].replace(')',''))

class configureAWS:
    host = ...
    rootCAPath = ...
    certificatePath = ...
    privateKeyPath = ...
    clientId = ...
    topic_rover = ...
    useWebsocket=False

                    
    def __init__(self):
        pass

    def get_aws_host(self):
        return self.host
    def set_aws_host(self, aws_host):
        self.host = aws_host    
        
    def get_root_file(self):
        return self.rootCAPath
    def set_root_file(self, root_file_path):
        self.rootCAPath = root_file_path
        
    def get_cert_file(self):
        return self.certificatePath
    def set_cert_file(self, cert_file_path):
        self.certificatePath = cert_file_path

    def get_priv_file(self):
        return self.privateKeyPath
    def set_priv_file(self, priv_file_path):
        self.privateKeyPath = priv_file_path        
        
    def get_thing_name(self):
        return self.clientId
    def set_thing_name(self, thing_name):
        self.clientId = thing_name

    def get_topic(self, keyword):
        if keyword=="rover":
            return self.topic_rover
        if keyword=="target":
            return self.topic_target
        else:
            return 'default/topic'
    def set_topic(self, keyword):
        if keyword=="rover":
            self.topic_rover = keyword
        if keyword=="target":
            self.topic_target= keyword
    

myAWSConfig=configureAWS()
host = myAWSConfig.get_aws_host()
rootCAPath = myAWSConfig.get_root_file()
certificatePath = myAWSConfig.get_cert_file()
privateKeyPath = myAWSConfig.get_priv_file()
clientId = myAWSConfig.get_thing_name()
topic_rover = myAWSConfig.get_topic("rover")
useWebsocket=myAWSConfig.useWebsocket

def connect_aws():

    if useWebsocket and certificatePath and privateKeyPath:
        print("X.509 cert authentication and WebSocket are mutual exclusive. Please pick one.")
        exit(2)

    if not useWebsocket and (not certificatePath or not privateKeyPath):
        print("Missing credentials for authentication.")
        exit(2)

# Port defaults
    if useWebsocket:  # When no port override for WebSocket, default to 443
        port = 443
    if not useWebsocket:  # When no port override for non-WebSocket, default to 8883
        port = 8883 
    
    
    myAWSIoTMQTTClient = None
    if useWebsocket:
        myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId, useWebsocket=True)
        myAWSIoTMQTTClient.configureEndpoint(host, port)
        myAWSIoTMQTTClient.configureCredentials(rootCAPath)
    else:
        myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId)
        myAWSIoTMQTTClient.configureEndpoint(host, port)
        myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)

    # AWSIoTMQTTClient connection configuration
    myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20)
    myAWSIoTMQTTClient.configureOfflinePublishQueueing(-1)  # Infinite offline Publish queueing
    myAWSIoTMQTTClient.configureDrainingFrequency(2)  # Draining: 2 Hz
    myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10)  # 10 sec
    myAWSIoTMQTTClient.configureMQTTOperationTimeout(5)  # 5 sec

# Connect and subscribe to AWS IoT
    myAWSIoTMQTTClient.connect()
    return myAWSIoTMQTTClient
                     

if __name__ == "__main__":
 
    myAWSIoTMQTTClient=connect_aws()    
    myAWSIoTMQTTClient.subscribe(topic_rover, 1, customCallback)    
    print("subscribed")

    while(True):
        print(coordinateX)
如果我尝试在回调函数中打印坐标,它会打印正确的值,但在while循环的main函数中,它只打印0

代码可能会有一些开销和一些可能没有意义的东西,但请忽略这一点,我很快就把它们放在一起,重要的是它可以工作(除了我不能工作的部分lol)

我确信我错过了非常简单的soemthing,但我对Python还是相当陌生的