-
Notifications
You must be signed in to change notification settings - Fork 53
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
Newer example code #142
base: master
Are you sure you want to change the base?
Newer example code #142
Conversation
This PR provides a new revision of the sample code that has a few key features: * construct the gmqtt.Client() outside of the run loop * graceful shutdown of the client, and clearing of all tasks so that `loop.close()` does not throw errors * switch to `.run_forever()` instead of the singular gmqtt task, providing an example for *other* tasks to run in the same event loop * removal of the STOP event, in favor of the loop's built-in stopping mechanism
Remove accidental change. "client-id" has a clearer intent, so restore that.
README.md
Outdated
# Inject a message, once the connection is established. | ||
def publish(fut): | ||
client.publish('TEST/TIME', str(time.time()), qos=1) | ||
t.add_done_callback(publish) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More explicit way to do so is to place publish in on_connect callback 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the idea. Moved!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An unexpected change: upon a reconnect, another test message is published. The "done callback" approach only publishes a single message. ... That said, I prefer the on_connect approach for sample code, as it is simpler to understand for the reader.
Rather than using a task's "done callbacks", we can deliver the test message once the connection is established. Clean that out in the "main" code. Remove an unused publish() function.
This PR provides a new revision of the sample code that has a few key features:
loop.close()
does not throw errors.run_forever()
instead of the singular gmqtt task, providing an example for other tasks to run in the same event loop