Skip to content

Commit a289cbf

Browse files
committed
Delay_ms: trig after deinit raises exception.
1 parent e02bd0c commit a289cbf

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

v3/docs/TUTORIAL.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ REPL.
4343
     4.1.2 [Portable code](./TUTORIAL.md#412-portable-code)
4444
4.2 [Asynchronous iterators](./TUTORIAL.md#42-asynchronous-iterators)
4545
4.3 [Asynchronous context managers](./TUTORIAL.md#43-asynchronous-context-managers)
46+
4.4 [Object scope](./TUTORIAL.md#44-object-scope) What happens when an object goes out of scope.
4647
5. [Exceptions timeouts and cancellation](./TUTORIAL.md#5-exceptions-timeouts-and-cancellation)
4748
5.1 [Exceptions](./TUTORIAL.md#51-exceptions)
4849
     5.1.1 [Global exception handler](./TUTORIAL.md#511-global-exception-handler)
@@ -1134,7 +1135,7 @@ Methods:
11341135
proceed when the instance has timed out.
11351136
7. `callback` args `func=None`, `args=()`. Allows the callable and its args to
11361137
be assigned, reassigned or disabled at run time.
1137-
8. `deinit` No args. Cancels the coroutine.
1138+
8. `deinit` No args. Cancels the running task. See [Object scope](./TUTORIAL.md#44-object-scope).
11381139

11391140
In this example a `Delay_ms` instance is created with the default duration of
11401141
1s. It is repeatedly triggered for 5 secs, preventing the callback from
@@ -1522,6 +1523,16 @@ asyncio.run(bar())
15221523

15231524
###### [Contents](./TUTORIAL.md#contents)
15241525

1526+
## 4.4 Object scope
1527+
1528+
If an object launches a task and that object goes out of scope, the task will
1529+
continue to be scheduled. The task will run to completion or until cancelled.
1530+
If this is undesirable consider writing a `deinit` method to cancel associated
1531+
running tasks. Applications can call `deinit`, for example in a `try...finally`
1532+
block or in a context manager.
1533+
1534+
###### [Contents](./TUTORIAL.md#contents)
1535+
15251536
# 5 Exceptions timeouts and cancellation
15261537

15271538
These topics are related: `uasyncio` enables the cancellation of tasks, and the

v3/primitives/delay_ms.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ async def _timer(self, dt):
4848
# API
4949
# trigger may be called from hard ISR.
5050
def trigger(self, duration=0): # Update absolute end time, 0-> ctor default
51+
if self._mtask is None:
52+
raise RuntimeError("Delay_ms.deinit() has run.")
5153
self._tend = ticks_add(ticks_ms(), duration if duration > 0 else self._durn)
5254
self._retn = None # Default in case cancelled.
5355
self._busy = True
@@ -73,3 +75,4 @@ def callback(self, func=None, args=()):
7375
def deinit(self):
7476
self.stop()
7577
self._mtask.cancel()
78+
self._mtask = None

v3/primitives/tests/delay_test.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ async def reduce_test(): # Test reducing a running delay
9191
Callback should run
9292
cb callback
9393
Callback should run
94+
cb callback
9495
Done
9596
'''
9697
printexp(s, 11)
@@ -174,6 +175,31 @@ def timer_cb(_):
174175
await asyncio.sleep(1)
175176
print('Done')
176177

178+
async def err_test(): # Test triggering de-initialised timer
179+
s = '''
180+
Running (runtime = 3s):
181+
Trigger 1 sec delay
182+
cb callback
183+
Success: error was raised.
184+
Done
185+
'''
186+
printexp(s, 3)
187+
def cb(v):
188+
print('cb', v)
189+
return 42
190+
191+
d = Delay_ms(cb, ('callback',))
192+
193+
print('Trigger 1 sec delay')
194+
d.trigger(1000)
195+
await asyncio.sleep(2)
196+
d.deinit()
197+
try:
198+
d.trigger(1000)
199+
except RuntimeError:
200+
print("Success: error was raised.")
201+
print('Done')
202+
177203
av = '''
178204
Run a test by issuing
179205
delay_test.test(n)
@@ -184,11 +210,12 @@ def timer_cb(_):
184210
2 Test reducing the duration of a running timer
185211
3 Test delay defined by constructor arg
186212
4 Test triggering a Task
213+
5 Attempt to trigger de-initialised instance
187214
\x1b[39m
188215
'''
189216
print(av)
190217

191-
tests = (isr_test, stop_test, reduce_test, ctor_test, launch_test)
218+
tests = (isr_test, stop_test, reduce_test, ctor_test, launch_test, err_test)
192219
def test(n=0):
193220
try:
194221
asyncio.run(tests[n]())

0 commit comments

Comments
 (0)