ch20 examples
This commit is contained in:
@@ -1,58 +0,0 @@
|
|||||||
# spinner_async_experiment.py
|
|
||||||
|
|
||||||
# credits: Example by Luciano Ramalho inspired by
|
|
||||||
# Michele Simionato's multiprocessing example in the python-list:
|
|
||||||
# https://mail.python.org/pipermail/python-list/2009-February/675659.html
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import itertools
|
|
||||||
import math
|
|
||||||
|
|
||||||
# tag::SPINNER_ASYNC_NAP[]
|
|
||||||
async def is_prime(n):
|
|
||||||
if n < 2:
|
|
||||||
return False
|
|
||||||
if n == 2:
|
|
||||||
return True
|
|
||||||
if n % 2 == 0:
|
|
||||||
return False
|
|
||||||
|
|
||||||
sleep = asyncio.sleep # <1>
|
|
||||||
root = math.floor(math.sqrt(n))
|
|
||||||
for i in range(3, root + 1, 2):
|
|
||||||
if n % i == 0:
|
|
||||||
return False
|
|
||||||
if i % 100_000 == 1: # <2>
|
|
||||||
await sleep(0)
|
|
||||||
return True
|
|
||||||
# end::SPINNER_ASYNC_NAP[]
|
|
||||||
|
|
||||||
|
|
||||||
async def spin(msg: str) -> None:
|
|
||||||
for char in itertools.cycle(r'\|/-'):
|
|
||||||
status = f'\r{char} {msg}'
|
|
||||||
print(status, flush=True, end='')
|
|
||||||
try:
|
|
||||||
await asyncio.sleep(.1)
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
break
|
|
||||||
blanks = ' ' * len(status)
|
|
||||||
print(f'\r{blanks}\r', end='')
|
|
||||||
|
|
||||||
async def slow() -> int:
|
|
||||||
await is_prime(5_000_111_000_222_021) # <4>
|
|
||||||
return 42
|
|
||||||
|
|
||||||
async def supervisor() -> int:
|
|
||||||
spinner = asyncio.create_task(spin('thinking!')) # <1>
|
|
||||||
print('spinner object:', spinner) # <2>
|
|
||||||
result = await slow() # <3>
|
|
||||||
spinner.cancel() # <5>
|
|
||||||
return result
|
|
||||||
|
|
||||||
def main() -> None:
|
|
||||||
result = asyncio.run(supervisor())
|
|
||||||
print('Answer:', result)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
||||||
41
20-concurrency/spinner_async.py
Normal file
41
20-concurrency/spinner_async.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# spinner_async.py
|
||||||
|
|
||||||
|
# credits: Example by Luciano Ramalho inspired by
|
||||||
|
# Michele Simionato's multiprocessing example in the python-list:
|
||||||
|
# https://mail.python.org/pipermail/python-list/2009-February/675659.html
|
||||||
|
|
||||||
|
# tag::SPINNER_ASYNC_TOP[]
|
||||||
|
import asyncio
|
||||||
|
import itertools
|
||||||
|
|
||||||
|
async def spin(msg: str) -> None: # <1>
|
||||||
|
for char in itertools.cycle(r'\|/-'):
|
||||||
|
status = f'\r{char} {msg}'
|
||||||
|
print(status, flush=True, end='')
|
||||||
|
try:
|
||||||
|
await asyncio.sleep(.1) # <2>
|
||||||
|
except asyncio.CancelledError: # <3>
|
||||||
|
break
|
||||||
|
blanks = ' ' * len(status)
|
||||||
|
print(f'\r{blanks}\r', end='')
|
||||||
|
|
||||||
|
async def slow() -> int:
|
||||||
|
await asyncio.sleep(3) # <4>
|
||||||
|
return 42
|
||||||
|
# end::SPINNER_ASYNC_TOP[]
|
||||||
|
|
||||||
|
# tag::SPINNER_ASYNC_START[]
|
||||||
|
def main() -> None: # <1>
|
||||||
|
result = asyncio.run(supervisor()) # <2>
|
||||||
|
print('Answer:', result)
|
||||||
|
|
||||||
|
async def supervisor() -> int: # <3>
|
||||||
|
spinner = asyncio.create_task(spin('thinking!')) # <4>
|
||||||
|
print('spinner object:', spinner) # <5>
|
||||||
|
result = await slow() # <6>
|
||||||
|
spinner.cancel() # <7>
|
||||||
|
return result
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
# end::SPINNER_ASYNC_START[]
|
||||||
@@ -6,8 +6,7 @@
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import itertools
|
import itertools
|
||||||
|
import time
|
||||||
import primes
|
|
||||||
|
|
||||||
async def spin(msg: str) -> None:
|
async def spin(msg: str) -> None:
|
||||||
for char in itertools.cycle(r'\|/-'):
|
for char in itertools.cycle(r'\|/-'):
|
||||||
@@ -21,7 +20,7 @@ async def spin(msg: str) -> None:
|
|||||||
|
|
||||||
# tag::SPINNER_ASYNC_EXPERIMENT[]
|
# tag::SPINNER_ASYNC_EXPERIMENT[]
|
||||||
async def slow() -> int:
|
async def slow() -> int:
|
||||||
primes.is_prime(5_000_111_000_222_021) # <4>
|
time.sleep(3) # <4>
|
||||||
return 42
|
return 42
|
||||||
|
|
||||||
async def supervisor() -> int:
|
async def supervisor() -> int:
|
||||||
47
20-concurrency/spinner_proc.py
Normal file
47
20-concurrency/spinner_proc.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
# spinner_proc.py
|
||||||
|
|
||||||
|
# credits: Adapted from Michele Simionato's
|
||||||
|
# multiprocessing example in the python-list:
|
||||||
|
# https://mail.python.org/pipermail/python-list/2009-February/675659.html
|
||||||
|
|
||||||
|
# tag::SPINNER_PROC_IMPORTS[]
|
||||||
|
from multiprocessing import Process, Event # <1>
|
||||||
|
from multiprocessing import synchronize # <2>
|
||||||
|
import itertools
|
||||||
|
import time
|
||||||
|
|
||||||
|
def spin(msg: str, done: synchronize.Event) -> None: # <3>
|
||||||
|
# end::SPINNER_PROC_IMPORTS[]
|
||||||
|
for char in itertools.cycle(r'\|/-'):
|
||||||
|
status = f'\r{char} {msg}'
|
||||||
|
print(status, end='', flush=True)
|
||||||
|
if done.wait(.1):
|
||||||
|
break
|
||||||
|
blanks = ' ' * len(status)
|
||||||
|
print(f'\r{blanks}\r', end='')
|
||||||
|
|
||||||
|
def slow() -> int:
|
||||||
|
time.sleep(3)
|
||||||
|
return 42
|
||||||
|
|
||||||
|
# tag::SPINNER_PROC_SUPER[]
|
||||||
|
def supervisor() -> int:
|
||||||
|
done = Event()
|
||||||
|
spinner = Process(target=spin, # <4>
|
||||||
|
args=('thinking!', done))
|
||||||
|
print('spinner object:', spinner) # <5>
|
||||||
|
spinner.start()
|
||||||
|
result = slow()
|
||||||
|
done.set()
|
||||||
|
spinner.join()
|
||||||
|
return result
|
||||||
|
# end::SPINNER_PROC_SUPER[]
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
result = supervisor()
|
||||||
|
print('Answer:', result)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
||||||
@@ -7,8 +7,7 @@
|
|||||||
# tag::SPINNER_THREAD_TOP[]
|
# tag::SPINNER_THREAD_TOP[]
|
||||||
from threading import Thread, Event
|
from threading import Thread, Event
|
||||||
import itertools
|
import itertools
|
||||||
|
import time
|
||||||
from primes import is_prime
|
|
||||||
|
|
||||||
def spin(msg: str, done: Event) -> None: # <1>
|
def spin(msg: str, done: Event) -> None: # <1>
|
||||||
for char in itertools.cycle(r'\|/-'): # <2>
|
for char in itertools.cycle(r'\|/-'): # <2>
|
||||||
@@ -20,7 +19,7 @@ def spin(msg: str, done: Event) -> None: # <1>
|
|||||||
print(f'\r{blanks}\r', end='') # <6>
|
print(f'\r{blanks}\r', end='') # <6>
|
||||||
|
|
||||||
def slow() -> int:
|
def slow() -> int:
|
||||||
is_prime(5_000_111_000_222_021) # <7>
|
time.sleep(3) # <7>
|
||||||
return 42
|
return 42
|
||||||
# end::SPINNER_THREAD_TOP[]
|
# end::SPINNER_THREAD_TOP[]
|
||||||
|
|
||||||
Reference in New Issue
Block a user