Skip to content

Commit 8152d1b

Browse files
committed
PRIMITIVES.md Add Gather usage example.
1 parent 3af09c3 commit 8152d1b

File tree

1 file changed

+75
-50
lines changed

1 file changed

+75
-50
lines changed

PRIMITIVES.md

Lines changed: 75 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,32 @@ obvious workround is to produce a version with unused primitives removed.
1212

1313
# Contents
1414

15-
1. [The asyn.py library](./PRIMITIVES.md#1-the-asyn.py-library)
16-
17-
1.1 [Synchronisation Primitives](./PRIMITIVES.md#11-synchronisation-primitives)
18-
19-
1.2 [Task control and monitoring](./PRIMITIVES.md#12-task-control-and-monitoring)
20-
21-
2. [Modules](./PRIMITIVES.md#2-modules)
22-
23-
3 [Synchronisation Primitives](./PRIMITIVES.md#3-synchronisation-primitives)
24-
25-
3.1 [Function launch](./PRIMITIVES.md#31-function-launch)
26-
27-
3.2 [Class Lock](./PRIMITIVES.md#32-class-lock)
28-
29-
3.2.1 [Definition](./PRIMITIVES.md#321-definition)
30-
31-
3.3 [Class Event](./PRIMITIVES.md#33-class-event)
32-
33-
3.3.1 [Definition](./PRIMITIVES.md#331-definition)
34-
35-
3.4 [Class Barrier](./PRIMITIVES.md#34-class-barrier)
36-
37-
3.5 [Class Semaphore](./PRIMITIVES.md#35-class-semaphore)
38-
39-
3.5.1 [Class BoundedSemaphore](./PRIMITIVES.md#351-class-boundedsemaphore)
40-
41-
3.6 [Class Condition](./PRIMITIVES.md#36-class-condition)
42-
43-
3.6.1 [Definition](./PRIMITIVES.md#361-definition)
44-
45-
3.7 [Class Gather](./PRIMITIVES.md#37-class-gather)
46-
47-
3.7.1 [Definition](./PRIMITIVES.md#371-definition)
48-
49-
4. [Task Cancellation](./PRIMITIVES.md#4-task-cancellation)
50-
51-
4.1 [Coro sleep](./PRIMITIVES.md#41-coro-sleep)
52-
53-
4.2 [Class Cancellable](./PRIMITIVES.md#42-class-cancellable)
54-
55-
4.2.1 [Groups](./PRIMITIVES.md#421-groups)
56-
57-
4.2.2 [Custom cleanup](./PRIMITIVES.md#422-custom-cleanup)
58-
59-
4.3 [Class NamedTask](./PRIMITIVES.md#43-class-namedtask)
60-
61-
4.3.1 [Latency and Barrier objects](./PRIMITIVES.md#431-latency-and-barrier-objects)
62-
63-
4.3.2 [Custom cleanup](./PRIMITIVES.md#432-custom-cleanup)
64-
15+
1. [The asyn.py library](./PRIMITIVES.md#1-the-asyn.py-library)
16+
1.1 [Synchronisation Primitives](./PRIMITIVES.md#11-synchronisation-primitives)
17+
1.2 [Task control and monitoring](./PRIMITIVES.md#12-task-control-and-monitoring)
18+
2. [Modules](./PRIMITIVES.md#2-modules)
19+
3 [Synchronisation Primitives](./PRIMITIVES.md#3-synchronisation-primitives)
20+
3.1 [Function launch](./PRIMITIVES.md#31-function-launch)
21+
3.2 [Class Lock](./PRIMITIVES.md#32-class-lock)
22+
3.2.1 [Definition](./PRIMITIVES.md#321-definition)
23+
3.3 [Class Event](./PRIMITIVES.md#33-class-event)
24+
3.3.1 [Definition](./PRIMITIVES.md#331-definition)
25+
3.4 [Class Barrier](./PRIMITIVES.md#34-class-barrier)
26+
3.5 [Class Semaphore](./PRIMITIVES.md#35-class-semaphore)
27+
3.5.1 [Class BoundedSemaphore](./PRIMITIVES.md#351-class-boundedsemaphore)
28+
3.6 [Class Condition](./PRIMITIVES.md#36-class-condition)
29+
3.6.1 [Definition](./PRIMITIVES.md#361-definition)
30+
3.7 [Class Gather](./PRIMITIVES.md#37-class-gather)
31+
3.7.1 [Definition](./PRIMITIVES.md#371-definition)
32+
3.7.2 [Use with timeouts and cancellation](./PRIMITIVES.md#372-use-with-timeouts-and-cancellation)
33+
4. [Task Cancellation](./PRIMITIVES.md#4-task-cancellation)
34+
4.1 [Coro sleep](./PRIMITIVES.md#41-coro-sleep)
35+
4.2 [Class Cancellable](./PRIMITIVES.md#42-class-cancellable)
36+
4.2.1 [Groups](./PRIMITIVES.md#421-groups)
37+
4.2.2 [Custom cleanup](./PRIMITIVES.md#422-custom-cleanup)
38+
4.3 [Class NamedTask](./PRIMITIVES.md#43-class-namedtask)
39+
4.3.1 [Latency and Barrier objects](./PRIMITIVES.md#431-latency-and-barrier-objects)
40+
4.3.2 [Custom cleanup](./PRIMITIVES.md#432-custom-cleanup)
6541
4.3.3 [Changes](./PRIMITIVES.md#433-changes)
6642

6743
## 1.1 Synchronisation Primitives
@@ -377,6 +353,10 @@ list of results returned by the tasks. Timeouts may be assigned to individual
377353
tasks.
378354

379355
```python
356+
async def foo(n):
357+
await asyncio.sleep(n)
358+
return n * n
359+
380360
async def bar(x, y, rats): # Example coro: note arg passing
381361
await asyncio.sleep(1)
382362
return x * y * rats
@@ -411,6 +391,51 @@ matches the length of the list of `Gatherable` instances. Each element contains
411391
the return value of the corresponding `Gatherable` instance. Each return value
412392
may be of any type.
413393

394+
### 3.7.2 Use with timeouts and cancellation
395+
396+
The following complete example illustrates the use of `Gather` with tasks which
397+
are subject to cancellation or timeout.
398+
399+
```python
400+
import uasyncio as asyncio
401+
import asyn
402+
403+
async def foo(n):
404+
while True:
405+
try:
406+
await asyncio.sleep(1)
407+
n += 1
408+
except asyncio.TimeoutError:
409+
print('foo timeout')
410+
return n
411+
412+
@asyn.cancellable
413+
async def bar(n):
414+
while True:
415+
try:
416+
await asyncio.sleep(1)
417+
n += 1
418+
except asyn.StopTask:
419+
print('bar stopped')
420+
return n
421+
422+
async def do_cancel():
423+
await asyncio.sleep(5)
424+
await asyn.Cancellable.cancel_all()
425+
426+
427+
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))
431+
loop.create_task(do_cancel())
432+
res = await asyn.Gather(gatherables)
433+
print('Result: ', res)
434+
435+
loop = asyncio.get_event_loop()
436+
loop.run_until_complete(main(loop))
437+
```
438+
414439
###### [Contents](./PRIMITIVES.md#contents)
415440

416441
# 4. Task Cancellation

0 commit comments

Comments
 (0)