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

Newer example code #142

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Newer example code #142

wants to merge 3 commits into from

Conversation

gstein
Copy link

@gstein gstein commented Feb 7, 2022

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

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)
Copy link
Collaborator

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 🙂

Copy link
Author

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!

Copy link
Author

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.
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

Successfully merging this pull request may close these issues.

None yet

2 participants