Open
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.
Lenka42
reviewed
Feb 8, 2022
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) |
Collaborator
There was a problem hiding this comment.
More explicit way to do so is to place publish in on_connect callback 🙂
Author
There was a problem hiding this comment.
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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