Skip to content

Newer example code#142

Open
gstein wants to merge 3 commits intowialon:masterfrom
gstein:patch-1
Open

Newer example code#142
gstein wants to merge 3 commits intowialon:masterfrom
gstein:patch-1

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.

2 participants