amqtt/samples/client_keepalive.py

53 wiersze
1.1 KiB
Python
Czysty Zwykły widok Historia

2015-07-05 13:53:02 +00:00
import asyncio
import logging
from asyncio import CancelledError
2015-07-05 13:53:02 +00:00
2021-03-27 12:59:48 +00:00
from amqtt.client import MQTTClient
2015-07-07 19:55:17 +00:00
"""
This sample shows how to run an idle client
"""
2015-07-06 20:20:05 +00:00
2015-07-05 13:53:02 +00:00
logger = logging.getLogger(__name__)
config = {
"keep_alive": 5,
"ping_delay": 1,
2015-07-05 13:53:02 +00:00
}
async def main() -> None:
client = MQTTClient(config=config)
try:
await client.connect("mqtt://test.mosquitto.org:1883/")
logger.info("client connected")
await asyncio.sleep(15)
except CancelledError:
pass
await client.disconnect()
def __main__():
formatter = "[%(asctime)s] :: %(levelname)s :: %(name)s :: %(message)s"
logging.basicConfig(level=logging.INFO, format=formatter)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
2017-08-06 22:06:57 +00:00
task = loop.create_task(main())
2015-07-05 13:53:02 +00:00
try:
loop.run_until_complete(task)
except KeyboardInterrupt:
logger.info("KeyboardInterrupt received. Stopping client...")
task.cancel()
loop.run_until_complete(task) # Ensure task finishes cleanup
finally:
2015-07-05 13:53:02 +00:00
loop.close()
2015-07-05 13:53:02 +00:00
if __name__ == "__main__":
__main__()