@@ -96,7 +96,8 @@ be specified to run forever, once only or a fixed number of times. `schedule`
96
96
is an asynchronous function.
97
97
98
98
Positional args:
99
- 1 . ` func ` The callable (callback or coroutine) to run.
99
+ 1 . ` func ` The callable (callback or coroutine) to run. Alternatively an
100
+ ` Event ` may be passed (see below).
100
101
2 . Any further positional args are passed to the callable.
101
102
102
103
Keyword-only args. Args 1..6 are
@@ -154,6 +155,30 @@ try:
154
155
finally :
155
156
_ = asyncio.new_event_loop()
156
157
```
158
+ The event-based interface can be simpler than using callables:
159
+ ``` python
160
+ import uasyncio as asyncio
161
+ from sched.sched import schedule
162
+ from time import localtime
163
+
164
+ async def main ():
165
+ print (" Asynchronous test running..." )
166
+ evt = asyncio.Event()
167
+ asyncio.create_task(schedule(evt, hrs = 10 , mins = range (0 , 60 , 4 )))
168
+ while True :
169
+ await evt.wait() # Multiple tasks may wait on an Event
170
+ evt.clear() # It must be cleared.
171
+ yr, mo, md, h, m, s, wd = localtime()[:7 ]
172
+ print (f " Event { h:02d } : { m:02d } : { s:02d } on { md:02d } / { mo:02d } / { yr} " )
173
+
174
+ try :
175
+ asyncio.run(main())
176
+ finally :
177
+ _ = asyncio.new_event_loop()
178
+ ```
179
+ See [ tutorial] ( https://github.com/peterhinch/micropython-async/blob/master/v3/docs/TUTORIAL.md#32-event ) .
180
+ Also [ this doc] ( https://github.com/peterhinch/micropython-async/blob/master/v3/docs/EVENTS.md )
181
+ for a discussion of event-based programming.
157
182
158
183
##### [ Top] ( ./SCHEDULE.md#0-contents )
159
184
@@ -262,9 +287,10 @@ This is the core of the scheduler. Users of `uasyncio` do not need to concern
262
287
themseleves with it. It is documented for those wishing to modify the code and
263
288
for those wanting to perform scheduling in synchronous code.
264
289
265
- It is a closure whose creation accepts a time specification for future events.
266
- Each subsequent call is passed the current time and returns the number of
267
- seconds to wait for the next event to occur.
290
+ It is a closure whose creation accepts a time specification for future
291
+ triggers. When called it is passed a time value in seconds since the epoch. It
292
+ returns the number of seconds to wait for the next trigger to occur. It stores
293
+ no state.
268
294
269
295
It takes the following keyword-only args. A flexible set of data types are
270
296
accepted namely [ time specifiers] ( ./SCHEDULE.md#41-time-specifiers ) . Valid
@@ -404,14 +430,14 @@ def wait_for(**kwargs):
404
430
now = round (time())
405
431
scron = cron(** kwargs) # Cron instance for search.
406
432
while tim < now: # Find first event in sequence
407
- tim += scron(tim) + 2
408
- twait = tim - now - 600
433
+ # Defensive. scron should never return 0, but if it did the loop would never quit
434
+ tim += max (scron(tim), 1 )
435
+ twait = tim - now - 2 # Wait until 2 secs before first trigger
409
436
if twait > 0 :
410
437
sleep(twait)
411
- tcron = cron(** kwargs)
412
438
while True :
413
439
now = round (time())
414
- tw = tcron (now)
440
+ tw = scron (now)
415
441
sleep(tw + 2 )
416
442
```
417
443
0 commit comments