Skip to content

Commit d533d81

Browse files
committed
Tutorial: Impove section on Lock primitive.
1 parent f0054be commit d533d81

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

v3/docs/TUTORIAL.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ async def bar(x):
259259
await asyncio.sleep(1) # Pause 1s
260260

261261
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
263263
for x in range(3):
264264
tasks[x] = asyncio.create_task(bar(x))
265265
await asyncio.sleep(10)
@@ -351,7 +351,7 @@ async def bar(x):
351351
await asyncio.sleep(1) # Pause 1s
352352

353353
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
355355
for x in range(3):
356356
tasks[x] = asyncio.create_task(bar(x))
357357
print('Tasks are running')
@@ -621,12 +621,8 @@ The following provides a discussion of the primitives.
621621

622622
## 3.1 Lock
623623

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.
630626
```python
631627
from asyncio import Lock
632628
lock = Lock()
@@ -639,7 +635,10 @@ Asynchronous method:
639635
`await lock.acquire()`.
640636

641637
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.
643642
```python
644643
import asyncio
645644
from asyncio import Lock
@@ -652,7 +651,7 @@ async def task(i, lock):
652651

653652
async def main():
654653
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
656655
for n in range(1, 4):
657656
tasks[n - 1] = asyncio.create_task(task(n, lock))
658657
await asyncio.sleep(10)
@@ -680,7 +679,7 @@ async def task(i, lock):
680679

681680
async def main():
682681
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
684683
for n in range(1, 4):
685684
tasks[n - 1] = asyncio.create_task(task(n, lock))
686685
await asyncio.sleep(10)
@@ -911,7 +910,7 @@ async def foo(n, sema):
911910

912911
async def main():
913912
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
915914
for num in range(3):
916915
tasks[num] = asyncio.create_task(foo(num, sema))
917916
await asyncio.sleep(2)
@@ -1204,7 +1203,7 @@ async def main():
12041203
sw1 = asyncio.StreamWriter(UART(1, 9600), {})
12051204
sw2 = asyncio.StreamWriter(UART(2, 1200), {})
12061205
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
12081207
for n, sw in enumerate((sw1, sw2)):
12091208
tasks[n] = asyncio.create_task(sender(barrier, sw, n + 1))
12101209
await provider(barrier)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy