From fa6c80141336119fa94a585f54f706295cd44f65 Mon Sep 17 00:00:00 2001 From: David Dansby Date: Fri, 25 Oct 2019 21:21:47 -0700 Subject: [PATCH 1/2] - commit to test issues with git and test pycharm push to remote --- src/04-asyncio/loops/loops_uv/requirements.txt | 2 +- src/04-asyncio/web_scraping/async_scrape/program.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/04-asyncio/loops/loops_uv/requirements.txt b/src/04-asyncio/loops/loops_uv/requirements.txt index 99d4716..a63258a 100644 --- a/src/04-asyncio/loops/loops_uv/requirements.txt +++ b/src/04-asyncio/loops/loops_uv/requirements.txt @@ -1 +1 @@ -uvloop + uvloop diff --git a/src/04-asyncio/web_scraping/async_scrape/program.py b/src/04-asyncio/web_scraping/async_scrape/program.py index d8ed1ef..1da0f0c 100644 --- a/src/04-asyncio/web_scraping/async_scrape/program.py +++ b/src/04-asyncio/web_scraping/async_scrape/program.py @@ -8,6 +8,8 @@ # Make this available more easily. global loop +# this is a test to commit and push to repo through pycharm + async def get_html(episode_number: int) -> str: print(Fore.YELLOW + f"Getting HTML for episode {episode_number}", flush=True) From faee7d13a46d1bc5e0f23920723075a82cda1f3f Mon Sep 17 00:00:00 2001 From: David Dansby Date: Wed, 1 Apr 2020 02:39:45 -0700 Subject: [PATCH 2/2] formatting and small changes --- src/06-thread-safety/unsafe_bank.py | 9 +++++- src/09-built-on-asyncio/the_trio/prod_trio.py | 2 ++ .../acityscape_api/requirements.txt | 2 +- .../services/location_service.py | 2 +- .../acityscape_api/services/sun_service.py | 2 +- src/11-cython/perf/compute_threaded.py | 28 ++++++++++++++++++- src/11-cython/perf/math_core.pyx | 1 + 7 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/06-thread-safety/unsafe_bank.py b/src/06-thread-safety/unsafe_bank.py index 518d16f..6532b18 100644 --- a/src/06-thread-safety/unsafe_bank.py +++ b/src/06-thread-safety/unsafe_bank.py @@ -1,7 +1,7 @@ import datetime import random import time -from threading import Thread +from threading import Thread, RLock from typing import List @@ -55,14 +55,21 @@ def create_accounts() -> List[Account]: ] +transfer_lock = RLock() + def do_transfer(from_account: Account, to_account: Account, amount: int): if from_account.balance < amount: return + # Not so good: + transfer_lock.acquire() + from_account.balance -= amount time.sleep(.000) to_account.balance += amount + transfer_lock.release() + def validate_bank(accounts: List[Account], total: int, quiet=False): current = sum(a.balance for a in accounts) diff --git a/src/09-built-on-asyncio/the_trio/prod_trio.py b/src/09-built-on-asyncio/the_trio/prod_trio.py index a2362a5..54ae9c7 100644 --- a/src/09-built-on-asyncio/the_trio/prod_trio.py +++ b/src/09-built-on-asyncio/the_trio/prod_trio.py @@ -2,6 +2,7 @@ import colorama import random import trio +from trio._core import Nursery async def main(): @@ -12,6 +13,7 @@ async def main(): with trio.move_on_after(5): async with trio.open_nursery() as nursery: + nursery: Nursery = nursery nursery.start_soon(generate_data, 20, data, name='Prod 1') nursery.start_soon(generate_data, 20, data, name='Prod 2') nursery.start_soon(process_data, 40, data, name='Consumer') diff --git a/src/10-async-web/acityscape_api/requirements.txt b/src/10-async-web/acityscape_api/requirements.txt index ab6256a..cf7c820 100644 --- a/src/10-async-web/acityscape_api/requirements.txt +++ b/src/10-async-web/acityscape_api/requirements.txt @@ -8,4 +8,4 @@ cchardet # This one can give you trouble on Windows # Feel free to uncomment it but it also works without it # Just a little less well. -# aiodns +aiodns diff --git a/src/10-async-web/acityscape_api/services/location_service.py b/src/10-async-web/acityscape_api/services/location_service.py index ba56e08..77930e7 100644 --- a/src/10-async-web/acityscape_api/services/location_service.py +++ b/src/10-async-web/acityscape_api/services/location_service.py @@ -4,7 +4,7 @@ import aiohttp -use_cached_data = False +use_cached_data = True measured_latency_in_sec = [ 0.28844, 0.334694, 0.33468, 0.343911, 0.339515, 0.344329, 0.341594, 0.352366, diff --git a/src/10-async-web/acityscape_api/services/sun_service.py b/src/10-async-web/acityscape_api/services/sun_service.py index 70ef749..8da7ebb 100644 --- a/src/10-async-web/acityscape_api/services/sun_service.py +++ b/src/10-async-web/acityscape_api/services/sun_service.py @@ -6,7 +6,7 @@ import aiohttp measured_latency_in_sec = [0.399203, 0.7046, 0.422959, 0.741911, 0.404674] -use_cached_data = False +use_cached_data = True async def for_today(latitude: float, longitude: float) -> dict: diff --git a/src/11-cython/perf/compute_threaded.py b/src/11-cython/perf/compute_threaded.py index 83006a2..d34015a 100644 --- a/src/11-cython/perf/compute_threaded.py +++ b/src/11-cython/perf/compute_threaded.py @@ -27,7 +27,7 @@ def main(): dt = datetime.datetime.now() - t0 print("Done in {:,.2f} sec. (factor: {:,.2f}x)".format( dt.total_seconds(), - 8.54/dt.total_seconds()) + 8.54 / dt.total_seconds()) ) @@ -41,3 +41,29 @@ def do_math(start=0, num=10): if __name__ == '__main__': main() + +import logging +import threading +import time + + +def thread_function(name): + logging.info("Thread %s: starting", name) + time.sleep(2) + logging.info("Thread %s: finishing", name) + + +def main(): + format = "%(asctime)s: %(message)s" + logging.basicConfig(format=format, level=logging.INFO, + datefmt="%H:%M:%S") + + logging.info("Main : before creating thread") + x = threading.Thread(target=thread_function, args=(1,)) + logging.info("Main : before running thread") + x.start() + logging.info("Main : wait for the thread to finish") + x.join() + logging.info("Main : all done") + +main() \ No newline at end of file diff --git a/src/11-cython/perf/math_core.pyx b/src/11-cython/perf/math_core.pyx index 14b2a44..6abcba4 100644 --- a/src/11-cython/perf/math_core.pyx +++ b/src/11-cython/perf/math_core.pyx @@ -2,6 +2,7 @@ from libc.math cimport sqrt import cython + def do_math(start: cython.float = 0, num: cython.float = 10): pos: cython.float = start k_sq: cython.float = 1000 * 1000