Skip to content

Commit 2a3f42d

Browse files
committed
Incorporate @somacdavid's edits
1 parent 5072d99 commit 2a3f42d

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

asyncio-walkthrough/asyncq.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@
88
import time
99

1010

11-
async def makeitem(size=5):
11+
async def makeitem(size: int = 5) -> str:
1212
return os.urandom(size).hex()
1313

1414

15-
async def seconds():
15+
async def seconds() -> float:
1616
return time.perf_counter()
1717

1818

19-
async def randint(a, b):
19+
async def randint(a: int, b" int) -> int:
2020
return random.randint(a, b)
2121

2222

23-
async def randsleep(a=1, b=5, caller=None):
23+
async def randsleep(a: int = 1, b: int = 5, caller=None) -> None:
2424
i = await randint(a, b)
2525
if caller:
2626
print(f"{caller} sleeping for {i} seconds.")
2727
await asyncio.sleep(i)
2828

2929

30-
async def produce(name: int, q: asyncio.Queue):
30+
async def produce(name: int, q: asyncio.Queue) -> None:
3131
n = await randint(1, 5)
3232
for _ in it.repeat(None, n): # Synchronous
3333
await randsleep(caller=f"Producer {name}")
@@ -37,7 +37,7 @@ async def produce(name: int, q: asyncio.Queue):
3737
print(f"Producer {name} added <{i}> to queue.")
3838

3939

40-
async def consume(name: int, q: asyncio.Queue):
40+
async def consume(name: int, q: asyncio.Queue) -> None:
4141
while True:
4242
await randsleep(caller=f"Consumer {name}")
4343
i, t = await q.get()
@@ -48,7 +48,7 @@ async def consume(name: int, q: asyncio.Queue):
4848
q.task_done()
4949

5050

51-
async def main(nprod, ncon):
51+
async def main(nprod: int, ncon: int):
5252
q = asyncio.Queue()
5353
producers = [asyncio.create_task(produce(n, q)) for n in range(nprod)]
5454
consumers = [asyncio.create_task(consume(n, q)) for n in range(ncon)]

asyncio-walkthrough/chained.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import time
77

88

9-
async def randint(a, b):
9+
async def randint(a: int, b: int) -> int:
1010
return random.randint(a, b)
1111

1212

13-
async def part1(n):
13+
async def part1(n: int) -> str:
1414
i = await randint(0, 10)
1515
print(f"part1({n}) sleeping for {i} seconds.")
1616
await asyncio.sleep(i)
@@ -19,7 +19,7 @@ async def part1(n):
1919
return result
2020

2121

22-
async def part2(n, arg):
22+
async def part2(n: int, arg: str) -> str:
2323
i = await randint(0, 10)
2424
print(f"part2{n, arg} sleeping for {i} seconds.")
2525
await asyncio.sleep(i)
@@ -28,24 +28,24 @@ async def part2(n, arg):
2828
return result
2929

3030

31-
async def chain(n):
31+
async def chain(n: int) -> None:
3232
start = time.perf_counter()
3333
p1 = await part1(n)
3434
p2 = await part2(n, p1)
3535
end = time.perf_counter() - start
3636
print(f"-->Chained result{n} => {p2} (took {end:0.2f} seconds).")
3737

3838

39-
async def main(*ns):
40-
await asyncio.gather(*(chain(n) for n in ns))
39+
async def main(*args):
40+
await asyncio.gather(*(chain(n) for n in args))
4141

4242

4343
if __name__ == "__main__":
4444
import sys
4545

4646
random.seed(444)
47-
ns = [1, 2, 3] if len(sys.argv) == 1 else map(int, sys.argv[1:])
47+
args = [1, 2, 3] if len(sys.argv) == 1 else map(int, sys.argv[1:])
4848
start = time.perf_counter()
49-
asyncio.run(main(*ns))
49+
asyncio.run(main(*args))
5050
end = time.perf_counter() - start
5151
print(f"Program finished in {end:0.2f} seconds.")

asyncio-walkthrough/rand.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
)
1414

1515

16-
async def randint(a, b):
16+
async def randint(a: int, b: int) -> int:
1717
return random.randint(a, b)
1818

1919

20-
async def makerandom(idx, threshold=6):
20+
async def makerandom(idx: int, threshold: int = 6) -> int:
2121
print(c[idx + 1] + f"Initiated makerandom({idx}).")
2222
i = await randint(0, 10)
2323
while i <= threshold:

0 commit comments

Comments
 (0)