@@ -259,7 +259,7 @@ async def bar(x):
259
259
await asyncio.sleep(1 ) # Pause 1s
260
260
261
261
async def main ():
262
- tasks = [None ] * 3 # For CPython compaibility must store a reference see Note
262
+ tasks = [None ] * 3 # For CPython compaibility must store a reference see 2.2 Note
263
263
for x in range (3 ):
264
264
tasks[x] = asyncio.create_task(bar(x))
265
265
await asyncio.sleep(10 )
@@ -351,7 +351,7 @@ async def bar(x):
351
351
await asyncio.sleep(1 ) # Pause 1s
352
352
353
353
async def main ():
354
- tasks = [None ] * 3 # For CPython compaibility must store a reference see Note
354
+ tasks = [None ] * 3 # For CPython compaibility must store a reference see 2.2 Note
355
355
for x in range (3 ):
356
356
tasks[x] = asyncio.create_task(bar(x))
357
357
print (' Tasks are running' )
@@ -621,12 +621,8 @@ The following provides a discussion of the primitives.
621
621
622
622
## 3.1 Lock
623
623
624
- This describes the use of the official ` Lock ` primitive.
625
-
626
- This guarantees unique access to a shared resource. In the following code
627
- sample a ` Lock ` instance ` lock ` has been created and is passed to all tasks
628
- wishing to access the shared resource. Each task attempts to acquire the lock,
629
- pausing execution until it succeeds.
624
+ This describes the use of the official ` Lock ` primitive. This guarantees unique
625
+ access to a shared resource.
630
626
``` python
631
627
from asyncio import Lock
632
628
lock = Lock()
@@ -639,7 +635,10 @@ Asynchronous method:
639
635
` await lock.acquire() ` .
640
636
641
637
A task waiting on a lock may be cancelled or may be run subject to a timeout.
642
- The normal way to use a ` Lock ` is in a context manager:
638
+ The normal way to use a ` Lock ` is in a context manager. In the following code
639
+ sample a ` Lock ` instance ` lock ` has been created and is passed to all tasks
640
+ wishing to access the shared resource. Each task attempts to acquire the lock,
641
+ pausing execution until it succeeds.
643
642
``` python
644
643
import asyncio
645
644
from asyncio import Lock
@@ -652,7 +651,7 @@ async def task(i, lock):
652
651
653
652
async def main ():
654
653
lock = Lock() # The Lock instance
655
- tasks = [None ] * 3 # For CPython compaibility must store a reference see Note
654
+ tasks = [None ] * 3 # For CPython compaibility must store a reference see 2.2 Note
656
655
for n in range (1 , 4 ):
657
656
tasks[n - 1 ] = asyncio.create_task(task(n, lock))
658
657
await asyncio.sleep(10 )
@@ -680,7 +679,7 @@ async def task(i, lock):
680
679
681
680
async def main ():
682
681
lock = Lock() # The Lock instance
683
- tasks = [None ] * 3 # For CPython compaibility must store a reference see Note
682
+ tasks = [None ] * 3 # For CPython compaibility must store a reference see 2.2 Note
684
683
for n in range (1 , 4 ):
685
684
tasks[n - 1 ] = asyncio.create_task(task(n, lock))
686
685
await asyncio.sleep(10 )
@@ -911,7 +910,7 @@ async def foo(n, sema):
911
910
912
911
async def main ():
913
912
sema = Semaphore()
914
- tasks = [None ] * 3 # For CPython compaibility must store a reference see Note
913
+ tasks = [None ] * 3 # For CPython compaibility must store a reference see 2.2 Note
915
914
for num in range (3 ):
916
915
tasks[num] = asyncio.create_task(foo(num, sema))
917
916
await asyncio.sleep(2 )
@@ -1204,7 +1203,7 @@ async def main():
1204
1203
sw1 = asyncio.StreamWriter(UART(1 , 9600 ), {})
1205
1204
sw2 = asyncio.StreamWriter(UART(2 , 1200 ), {})
1206
1205
barrier = Barrier(3 )
1207
- tasks = [None ] * 2 # For CPython compaibility must store a reference see Note
1206
+ tasks = [None ] * 2 # For CPython compaibility must store a reference see 2.2 Note
1208
1207
for n, sw in enumerate ((sw1, sw2)):
1209
1208
tasks[n] = asyncio.create_task(sender(barrier, sw, n + 1 ))
1210
1209
await provider(barrier)
0 commit comments