Comments (6)
Async callbacks "subs_cb" have been discussed already: #8
so that you cannot await anything that publishes. I presume a workaround is something like:
That is the correct way to handle it. Either create a new async task for each incoming message or put them on a queue and start a consumer task.
but then you can't wait, catch exceptions, etc.
Not sure where you can't wait but you can definitely catch exceptions because that's what your "some_async_function()" can do. It can catch and process exceptions, just as you could in "subs_cb".
from micropython-mqtt.
Thanks. I should have said you can't conveniently await an async function directly in the callback. For example, suppose you need to publish two messages, one after the other, in your callback.
My solution using create_task
is of the "fire and forget" flavor. I guess the right approach for full functionality would be something like:
def cb(client):
args=...
queue.put(lambda: some_async_function(*args))
queue.put(lambda: some_other_async_function(*args))
async def run():
while True:
perform_sync_checks()
...
await asyncio.sleep_ms(loopDelay) #say 50ms
async def consume():
while True:
result = await queue.get() # Blocks until data is ready
await result()
def start():
global queue = Queue()
asyncio.create_task(consume())
asyncio.run(run())
...
from micropython-mqtt.
Why would you need to publish 2 messages in your callback?
Doing anything in the callback is a bad idea because it blocks the processing of further mqtt messages.
If you are interested in a complex example for processing mqtt messages, have a look at my mqtt handler: https://github.com/kevinkk525/pysmartnode/blob/a0998ad6582a28fe5a0529fb15dd4f61e254d25f/pysmartnode/networking/mqtt.py#L400
I don't understand what you are trying, so I'm kind of fishing blind.. (But since this isn't my repo, maybe @peterhinch understands your intentions)
from micropython-mqtt.
I can think of many scenarios where callbacks would result in multiple published messages or other asynchronous actions you’d like to wait on. But I do now understand the harm in doing that waiting in the callback itself. If you can sketch a concrete and simple suggested workaround, that may be useful for anyone in this situation who finds themselves reading here.
from micropython-mqtt.
Another approach would be to have the callback set an Event. The publish task would wait on the Event.
from micropython-mqtt.
Good idea thanks. Is there an example implementing this approach anywhere?
from micropython-mqtt.
Related Issues (20)
- config['gateway'] not defined - on Pi Pico W HOT 2
- Sporadic resets - not entirely clear why HOT 14
- MicroPython Unix port compatibility HOT 2
- publish to broker on an event trigger. HOT 2
- TypeError: object with buffer protocol required HOT 1
- wifi is treated as internal details only HOT 4
- can not handle 2 PINs at same time HOT 7
- access to the socket instead of the queue? HOT 2
- Memory allocation failures with MP 1.21 HOT 2
- malform packet and subnet traversal HOT 5
- Support for MQTTv5 HOT 46
- ESP32 MPv1.21.1 Wifi state goes briefly to IDLE after CONNECTING HOT 7
- Ethernet Support HOT 2
- ESP32 no reconnect on WiFi outage HOT 13
- ESP32 can't connect to wifi after soft reset HOT 3
- First time power up connection failed due to weak wifi HOT 4
- Issue with the memoryview HOT 3
- OpenSSL Error[0]: error:0A00010B:SSL routines::wrong version number HOT 1
- Unable to subscribe (Solved) HOT 2
- Esp8266 for wifi and mqtt HOT 2
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 micropython-mqtt.