Skip to content

Commit

Permalink
docs: Fix reconnection example; Close #288
Browse files Browse the repository at this point in the history
  • Loading branch information
empicano committed Mar 27, 2024
1 parent f7697de commit a867246
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build documentation
name: Documentation

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publish to PyPI
name: Publish

on:
release:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run tests
name: Tests

on:
push:
Expand Down
9 changes: 3 additions & 6 deletions docs/reconnection.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
# Reconnection

Network connections are inherently unstable and can fail at any time. Especially for long-running applications, this can be a challenge.
Network connections are inherently unstable and can fail at any time. Especially for long-running applications, this can be a challenge. To make our application resilient against connection failures, we can wrap the code in a `try`/`except`-block, listen for `MqttError`s, and reconnect like so:

To make an application resilient against connection failures, we have to be able to detect failures and recover from them.

The `Client` context is designed to be [reusable (but not reentrant)](https://docs.python.org/3/library/contextlib.html#reusable-context-managers). This means that we can wrap our code in a `try`/`except`-block, listen for `MqttError`s, and reconnect like so:
<!-- The `Client` context is designed to be [reusable (but not reentrant)](https://docs.python.org/3/library/contextlib.html#reusable-context-managers). -->

```python
import asyncio
import aiomqtt


async def main():
client = aiomqtt.Client("test.mosquitto.org")
interval = 5 # Seconds
while True:
try:
async with client:
async with aiomqtt.Client("test.mosquitto.org") as client:
await client.subscribe("humidity/#")
async for message in client.messages:
print(message.payload)
Expand Down

0 comments on commit a867246

Please sign in to comment.