Replies: 3 comments 15 replies
-
You could possibly use a standard asyncio.Event that all the consumers are blocked waiting for, the publisher has the single buffer that it updates then sets the event, which would wake up the consumers who'd then read the buffer. The problem then might be how the publisher knows when all consumers have processed the data before clearing the event for them to all go back to waiting again, maybe you could have a double buffer scenario instead; two buffers & two events, only one will ever be set at a time and consumers know that when they're woken for event1 they read buffer1 then immediately await on event2 etc, ping pong between the two. |
Beta Was this translation helpful? Give feedback.
-
@peterhinch When an async function that will be called on demand within in a program, one thing I'm not so clear on is whether to create the function as a task on program startup, using the async events set/clear flags initiate the function to run, and then to pause execution until its required again, or to just create the task anew each time the function is required to be executed. I suspect that theres no definitive 'rule' on which to base this and its probably of no consequence either way (I state this in blissful ignorance :) ) My somewhat limited understanding of this new message broker idea is that tasks or indeed other functions can be registered to a broker class which will then assign a topic id and set an async event, or create a new task, etc, when some other bit of the program publishes to that topic. The publisher can also send data when notifying the broker to set the required task (or whatever) in motion. I'm not too clear as yet on how that data gets translated into the aysnc functions parameters, but I think this will be clear if a few working examples are provided. Again, I not too clear on when one's program should choose to use the broker mechanism or just use the existing program techniques, but it does seem to be a nice way of handling the situation when there are a number of data collectors that need to fire off various tasks when the data is received. I look forward to having a go with the broker idea once you've had a chance to develop it, so I send this message more in support and interest about this development, though sadly not as one who can provide any sensible comments at the level required. But thanks for providing a very interesting development in the use of async programming. |
Beta Was this translation helpful? Give feedback.
-
The
Code has inevitably grown but at 59LOC it is still quite lightweight. Comments/code review welcome. @rkompass Re our discussion of queues - I think queue support is essential to the The We also discussed setting an [EDIT] Example of a queue which sets an class MyQueue(Agent, RingbufQueue):
def __init__(self, size):
RingbufQueue.__init__(self, size)
self.oflow = asyncio.Event()
def put(self, topic, message):
try:
self.put_nowait((topic, message))
except:
self.oflow.set() |
Beta Was this translation helpful? Give feedback.
-
I am looking for some kind of async broadcast channel for communication between async co-routines.
One of the co-routines drives my board's hardware, and is supposed to send and receive events to/from a web frontend.
So far, I have realized this with the
microdot
web server and an async websocket for communication with the web frontend. This works as intended.Of course, I do want to decouple the hardware driver from the websocket so that the former does continue to run when the websocket closes. So I am looking for a broadcast channel (or actually two) to connect my websocket(s) to the controlling routine.
This would be a single producer (potentially) multilple consumer channel to which the websocket tasks would subscribe, and vice versa for communication in the other direction. The channel only needs a tiny buffer that ideally overflows (does not block on sending).
Does something like that already exist in micropython (ESP32 port), or do I have to write my own code?
Beta Was this translation helpful? Give feedback.
All reactions