Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some problem with asyncio #185

Open
Hpeox opened this issue Oct 7, 2024 · 0 comments
Open

some problem with asyncio #185

Hpeox opened this issue Oct 7, 2024 · 0 comments

Comments

@Hpeox
Copy link

Hpeox commented Oct 7, 2024

I just try to return the rc from handle_connect using asyncio, but failed
the code:

@app.route('/connect/', methods=['POST','GET'])
async def make_connect():
    rc_status=-1
    connect_future = asyncio.Future()
    data = request.get_json()['data']
    print(data)
    app.config['MQTT_BROKER_URL'] = data['host']
    app.config['MQTT_BROKER_PORT'] = int(data['port'])
    if data['usr'] != '':
        app.config['MQTT_USERNAME'] = data['usr']
        app.config['MQTT_PASSWORD'] = data['password']
    app.config['MQTT_KEEPALIVE'] = 60
    app.config['MQTT_TLS_ENABLED'] = False
    print(connect_future.done())
    mqtt.init_app(app)

    @mqtt.on_connect()
    def handle_connect(client, userdata, flags, rc):
        if rc == 0:
            print('Connected successfully')
        # print(rc)
        print(connect_future.done())
        connect_future.set_result(rc)
        print(connect_future.done())
    
    result = await connect_future
    rc_status = result
    return {
        'rc_status':rc_status
    }

the output in terminal:

{'host': '127.0.0.1', 'port': '1883', 'usr': '', 'password': ''}
False
Connected successfully
False
True

so i think the connect_future is correctly set but something wrong happened
also i can't use ctrl-C to terminate the flask app correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant