Skip to content

Commit e6208b4

Browse files
committed
Minor doc improvements.
1 parent a73c387 commit e6208b4

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

PRIMITIVES.md

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -400,37 +400,46 @@ are subject to cancellation or timeout.
400400
import uasyncio as asyncio
401401
import asyn
402402

403+
async def barking(n):
404+
print('Start normal coro barking()')
405+
for _ in range(6):
406+
await asyncio.sleep(1)
407+
print('Done barking.')
408+
return 2 * n
409+
403410
async def foo(n):
404-
while True:
405-
try:
411+
print('Start timeout coro foo()')
412+
try:
413+
while True:
406414
await asyncio.sleep(1)
407415
n += 1
408-
except asyncio.TimeoutError:
409-
print('foo timeout')
410-
return n
416+
except asyncio.TimeoutError:
417+
print('foo timeout.')
418+
return n
411419

412420
@asyn.cancellable
413421
async def bar(n):
414-
while True:
415-
try:
422+
print('Start cancellable bar()')
423+
try:
424+
while True:
416425
await asyncio.sleep(1)
417426
n += 1
418-
except asyn.StopTask:
419-
print('bar stopped')
420-
return n
427+
except asyn.StopTask:
428+
print('bar stopped.')
429+
return n
421430

422431
async def do_cancel():
423-
await asyncio.sleep(5)
432+
await asyncio.sleep(5.5)
424433
await asyn.Cancellable.cancel_all()
425434

426-
427435
async def main(loop):
428-
bar_task = asyn.Cancellable(bar, 70)
429-
gatherables = [asyn.Gatherable(bar_task),]
430-
gatherables.append(asyn.Gatherable(foo, 10, timeout=7))
436+
bar_task = asyn.Cancellable(bar, 70) # Note args here
437+
gatherables = [asyn.Gatherable(barking, 21),
438+
asyn.Gatherable(foo, 10, timeout=7.5),
439+
asyn.Gatherable(bar_task)]
431440
loop.create_task(do_cancel())
432441
res = await asyn.Gather(gatherables)
433-
print('Result: ', res)
442+
print('Result: ', res) # Expect [42, 17, 75]
434443

435444
loop = asyncio.get_event_loop()
436445
loop.run_until_complete(main(loop))

TUTORIAL.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,15 +608,16 @@ controlled. Documentation of this is in the code.
608608

609609
## 3.6 Task cancellation
610610

611-
`uasyncio` now provides a `cancel(coro)` function. This works by throwing an
611+
`uasyncio` provides a `cancel(coro)` function. This works by throwing an
612612
exception to the coro in a special way: cancellation is deferred until the coro
613613
is next scheduled. This mechanism works with nested coros. However there is a
614614
limitation. If a coro issues `await uasyncio.sleep(secs)` or
615615
`uasyncio.sleep_ms(ms)` scheduling will not occur until the time has elapsed.
616616
This introduces latency into cancellation which matters in some use-cases.
617-
Other potential sources of latency take the form of slow code. `uasyncio` has
618-
no mechanism for verifying when cancellation has actually occurred. The `asyn`
619-
library provides verification via the following classes:
617+
Other potential sources of latency take the form of slow code.
618+
619+
`uasyncio` lacks a mechanism for verifying when cancellation has actually
620+
occurred. The `asyn` library provides verification via the following classes:
620621

621622
1. `Cancellable` This allows one or more tasks to be assigned to a group. A
622623
coro can cancel all tasks in the group, pausing until this has been achieved.

0 commit comments

Comments
 (0)