Comments (3)
Thanks for opening this issue. π Let me have a look. :)
I know in the advanced example it's using context manager and stuff like this. And the maintainer discourage the direct use of connect and disconnect calls.
Yes, I do. :) It was a design mistake on my part. Use the context manager interface instead.
However, in my current application, I want to use the client globally across different files.
Don't. :) It's an anti-pattern.
I am using it to publish messages inside http handler of a web framework. So I am not sure if a context manager would be feasible for this, and if so how to implement it.
Luckily for you, it is feasible. I assume that you use something like FastAPI. It supports context manager dependencies through dependency injection. This way, you can tie the lifetime of your single asyncio_mqtt.Client
instance to the life time of the fastapi.FastAPI
application.
Learn to love (async) context managers in python. They may seem like unnecessary boilerplate at first but they are definitely worth it in the long run. Just take my word for it. :)
Now to the actual issue at hand here: Reconnection.
The original paho library will just attempt reconnects on its own, once the connection has been lost, and for me as user nothing has to be done. I can keep using the same client I have initialized once. Maybe a publish will fail because the broker is currently not available, but soon after the broker is back the publish will succeed again. From my current tests this doesn't apply to this async client. I can shut down the broker and bring it back up, but the client will afterwards always report that it couldn't publish.
Yes, this is something that really makes paho-mqtt user friendly. Unfortunately, we (asyncio-mqtt) don't support automatic reconnection (yet). It's a trade-off between user friendliness and design complexity. IMHO, it's not worth the design complexity. I want to keep asyncio-mqtt leanβjust a small wrapper around paho-mqtt. Otherwise, I don't have the bandwidth to maintain it. Simple as that. :) If someone joins us (asyncio-mqtt) and share the maintenance burden, then we can consider automatic reconnection. π
Some of the required changes just on the top of my mind:
- We need to close #48 before we tackle reconnection. Maybe have both an immediate
publish
and apublish_and_wait
call as well. - Add
automatic_reconnection
keyword argument to theClient
. - If
automatic_reconnection==True
, we either use the existing paho infrastructure to do it (if feasible), or, do it ourselves with an internal loop.
If anyone us up for this, feel free to open a PR. π
from aiomqtt.
The context manager is tied to the lifecycle of the single request and not to the lifetime of the application. Please correct me if I am wrong.
You're right. I thought it was scoped to the application but it seems that it's scoped to each request.
Currently, I only see options to get proper reconnect behaviour if it's subscribing to something, since those are long-running coroutines which can be awaited.
Please provide an example of how to achieve the desired behaviour with a context manager.
I welcome anyone to make a pull request that adds this example. :)
Your question is about scoping and life time management. In other words, it's a question about how asyncio tasks work in general (and Python scope rules and import system in general). You need to find support for this elsewhere (e.g., stackoverflow.com).
asyncio-mqtt provides a general-purpose context manager interface. That is good enough for any use case. It's up to the user (you) to build on top of that. Yes, that currently includes reconnecting, connection pooling, etc. Whether your framework of choice (e.g., FastAPI) helps you with that or not is outside the scope of asyncio-mqtt.
This issue tracker is for specific bottlenecks/design choices/bugs in asyncio-mqtt that prevents you to build on top of it. I'll gladly review/accept PRs that improves these things. π This issue tracker is not for how python and asyncio works in general. I just don't have the bandwidth to answer general-purpose python/asyncio questions. I wish I had time for that but unfortunately I don't. :) Don't worry, there are other places (stackoverflow) where people seem to have plenty of time to answer your questions. π
I'll keep this issue open to address the issue of automatic reconnection because I think many people expect that. π Hopefully, someone wants to make a PR for the development and maintenance of this feature. :)
from aiomqtt.
Let's keep it constructive here. π
I'll re-open the issue to keep track of the reconnection feature. Again, if anyone searches for this, PRs are welcome. :)
from aiomqtt.
Related Issues (20)
- Impossible to reuse client in case of connection timeout HOT 7
- Typing warnings in IDE on Client methods HOT 2
- AttributeError: module 'aiomqtt' has no attribute '__version__'
- session persistence with protocol version 5 HOT 1
- Pin Paho to <2.0 HOT 2
- Make aiomqtt compatible with paho v2 HOT 10
- Topic.matches should be used when doing == HOT 4
- How to get the IP of a failed connection attempt
- strange reconnection HOT 2
- MQTT broker reports a disconnection/reconnection... but no aiomqtt.MqttError is raised HOT 4
- Exceptions on __aenter__ not handled. HOT 2
- Reconnect Bug HOT 1
- Cannot instantiate a client due to internal mqtt problem HOT 2
- Issues with uvicorn on Windows 10 HOT 5
- [BUG] Cannot install version 2.0.0 with paho-mqtt 2.0.0 HOT 2
- [BUG] 127.0.0.1/localhost Work Incorrectly HOT 3
- Do not send messages for a long time mqtt automatically disconnects HOT 1
- Can't read incoming messages in pytests HOT 8
- No convenient way to get message without getting locked into a for loop HOT 2
- Can aiomqtt queue has a ring buffer optionοΌ for high frequense in-comming messages? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aiomqtt.