-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal for an extension of module delay_ms #133
Comments
Have you seen the schedule module? This is my solution to scheduling tasks at regular intervals. |
No, not yet. I'll look at it. |
The schedule module does have an impressive functionality, which does what is needed in my current project, which is reading sensors (interval is typically 10 to 100 [s]) and reporting the results via MQTT (interval typically 100 to 1000 [s]). However I expect I will not (be able to) use it, as the underlying hardware is an ESP8266. I fear the schedule module is too big for this micro-controller. I still have to include an MQTT client (probably your asyncio client). As far as I understand it now, the use cases of the schedule module versus the proposed extension are quite different, with my current project being in the intersection of the two. The proposed extension is meant for cases in which no real time clock is needed or available, an interval of 0.01 to 1000 [s] is to be realized and the resource usage (RAM) is minimal. Thus I would still like to propose to implement the extension, for the benefit of users of very small micro-controllers. |
I have two reservations. Firstly I'm reluctant to increase the size of Secondly I think that the mission you wish to accomplish is best achieved with async code rather than by means of async def runner(t, evt):
while True:
await asyncio.sleep_ms(t)
evt.set() This could be used by multiple tasks as follows: async def this_job():
ev = asyncio.Event()
asyncio.create_task(runner(3000, ev)) # Runner instance
while True:
await ev.wait()
ev.clear()
# do work As a general comment the defining features of |
I like the approach you sketched: it is compact and bundles the definition and usage of a timer in a single coro. As a consequence I withdraw my proposal. After some tests, the following variant, which shows a better timing of the successive events, will be used in my project: class pre( asyncio.ThreadSafeFlag ): # Periodic repeating event
def __init__( self, t ):
super().__init__()
self.at= ticks_ms() # (Last) activation time
self.ts= t # Time step
async def runner( evt ):
while True:
now= ticks_ms()
while ticks_diff( evt.at, now ) <= 0:
evt.at= ticks_add( evt.at, evt.ts ) # Calculate time stamp of next event
await asyncio.sleep_ms( ticks_diff(evt.at,now) ) # Wait the remaining time
evt.set() It can be used in tasks in the following way: async def this_job():
ev= pre( 1000 )
asyncio.create_task( runner(ev) )
while True:
await ev.wait()
# Do something usefull Thank you very much. |
In most of my scripts running on micro-controllers, one or more tasks are to be scheduled at regular intervals, independent of the actual running time of the task at hand. To achieve this when using asyncio, a small extension to module
delay_ms
suffice. The difference between the official and the extended version of moduledelay_ms
is:Using this extension, an asynchronous task can be scheduled at regular intervals in the following way:
How about including this extension in module
delay_ms
?The text was updated successfully, but these errors were encountered: