Skip to content

Commit 7b193e0

Browse files
committed
ch20 examples
1 parent 916ceaa commit 7b193e0

File tree

5 files changed

+92
-64
lines changed

5 files changed

+92
-64
lines changed

20-concurrency/primes/spinner_async_nap.py

Lines changed: 0 additions & 58 deletions
This file was deleted.

20-concurrency/spinner_async.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# spinner_async.py
2+
3+
# credits: Example by Luciano Ramalho inspired by
4+
# Michele Simionato's multiprocessing example in the python-list:
5+
# https://mail.python.org/pipermail/python-list/2009-February/675659.html
6+
7+
# tag::SPINNER_ASYNC_TOP[]
8+
import asyncio
9+
import itertools
10+
11+
async def spin(msg: str) -> None: # <1>
12+
for char in itertools.cycle(r'\|/-'):
13+
status = f'\r{char} {msg}'
14+
print(status, flush=True, end='')
15+
try:
16+
await asyncio.sleep(.1) # <2>
17+
except asyncio.CancelledError: # <3>
18+
break
19+
blanks = ' ' * len(status)
20+
print(f'\r{blanks}\r', end='')
21+
22+
async def slow() -> int:
23+
await asyncio.sleep(3) # <4>
24+
return 42
25+
# end::SPINNER_ASYNC_TOP[]
26+
27+
# tag::SPINNER_ASYNC_START[]
28+
def main() -> None: # <1>
29+
result = asyncio.run(supervisor()) # <2>
30+
print('Answer:', result)
31+
32+
async def supervisor() -> int: # <3>
33+
spinner = asyncio.create_task(spin('thinking!')) # <4>
34+
print('spinner object:', spinner) # <5>
35+
result = await slow() # <6>
36+
spinner.cancel() # <7>
37+
return result
38+
39+
if __name__ == '__main__':
40+
main()
41+
# end::SPINNER_ASYNC_START[]

20-concurrency/primes/spinner_async_prime_no_spin.py renamed to 20-concurrency/spinner_async_experiment.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
import asyncio
88
import itertools
9-
10-
import primes
9+
import time
1110

1211
async def spin(msg: str) -> None:
1312
for char in itertools.cycle(r'\|/-'):
@@ -21,7 +20,7 @@ async def spin(msg: str) -> None:
2120

2221
# tag::SPINNER_ASYNC_EXPERIMENT[]
2322
async def slow() -> int:
24-
primes.is_prime(5_000_111_000_222_021) # <4>
23+
time.sleep(3) # <4>
2524
return 42
2625

2726
async def supervisor() -> int:

20-concurrency/spinner_proc.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# spinner_proc.py
2+
3+
# credits: Adapted from Michele Simionato's
4+
# multiprocessing example in the python-list:
5+
# https://mail.python.org/pipermail/python-list/2009-February/675659.html
6+
7+
# tag::SPINNER_PROC_IMPORTS[]
8+
from multiprocessing import Process, Event # <1>
9+
from multiprocessing import synchronize # <2>
10+
import itertools
11+
import time
12+
13+
def spin(msg: str, done: synchronize.Event) -> None: # <3>
14+
# end::SPINNER_PROC_IMPORTS[]
15+
for char in itertools.cycle(r'\|/-'):
16+
status = f'\r{char} {msg}'
17+
print(status, end='', flush=True)
18+
if done.wait(.1):
19+
break
20+
blanks = ' ' * len(status)
21+
print(f'\r{blanks}\r', end='')
22+
23+
def slow() -> int:
24+
time.sleep(3)
25+
return 42
26+
27+
# tag::SPINNER_PROC_SUPER[]
28+
def supervisor() -> int:
29+
done = Event()
30+
spinner = Process(target=spin, # <4>
31+
args=('thinking!', done))
32+
print('spinner object:', spinner) # <5>
33+
spinner.start()
34+
result = slow()
35+
done.set()
36+
spinner.join()
37+
return result
38+
# end::SPINNER_PROC_SUPER[]
39+
40+
def main() -> None:
41+
result = supervisor()
42+
print('Answer:', result)
43+
44+
45+
if __name__ == '__main__':
46+
main()
47+

20-concurrency/primes/spinner_thread.py renamed to 20-concurrency/spinner_thread.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
# tag::SPINNER_THREAD_TOP[]
88
from threading import Thread, Event
99
import itertools
10-
11-
from primes import is_prime
10+
import time
1211

1312
def spin(msg: str, done: Event) -> None: # <1>
1413
for char in itertools.cycle(r'\|/-'): # <2>
@@ -20,7 +19,7 @@ def spin(msg: str, done: Event) -> None: # <1>
2019
print(f'\r{blanks}\r', end='') # <6>
2120

2221
def slow() -> int:
23-
is_prime(5_000_111_000_222_021) # <7>
22+
time.sleep(3) # <7>
2423
return 42
2524
# end::SPINNER_THREAD_TOP[]
2625

0 commit comments

Comments
 (0)