Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit faee7d1

Browse files
committed
formatting and small changes
1 parent fa6c801 commit faee7d1

File tree

7 files changed

+41
-5
lines changed

7 files changed

+41
-5
lines changed

src/06-thread-safety/unsafe_bank.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
22
import random
33
import time
4-
from threading import Thread
4+
from threading import Thread, RLock
55
from typing import List
66

77

@@ -55,14 +55,21 @@ def create_accounts() -> List[Account]:
5555
]
5656

5757

58+
transfer_lock = RLock()
59+
5860
def do_transfer(from_account: Account, to_account: Account, amount: int):
5961
if from_account.balance < amount:
6062
return
6163

64+
# Not so good:
65+
transfer_lock.acquire()
66+
6267
from_account.balance -= amount
6368
time.sleep(.000)
6469
to_account.balance += amount
6570

71+
transfer_lock.release()
72+
6673

6774
def validate_bank(accounts: List[Account], total: int, quiet=False):
6875
current = sum(a.balance for a in accounts)

src/09-built-on-asyncio/the_trio/prod_trio.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import colorama
33
import random
44
import trio
5+
from trio._core import Nursery
56

67

78
async def main():
@@ -12,6 +13,7 @@ async def main():
1213

1314
with trio.move_on_after(5):
1415
async with trio.open_nursery() as nursery:
16+
nursery: Nursery = nursery
1517
nursery.start_soon(generate_data, 20, data, name='Prod 1')
1618
nursery.start_soon(generate_data, 20, data, name='Prod 2')
1719
nursery.start_soon(process_data, 40, data, name='Consumer')

src/10-async-web/acityscape_api/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ cchardet
88
# This one can give you trouble on Windows
99
# Feel free to uncomment it but it also works without it
1010
# Just a little less well.
11-
# aiodns
11+
aiodns

src/10-async-web/acityscape_api/services/location_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import aiohttp
66

7-
use_cached_data = False
7+
use_cached_data = True
88

99
measured_latency_in_sec = [
1010
0.28844, 0.334694, 0.33468, 0.343911, 0.339515, 0.344329, 0.341594, 0.352366,

src/10-async-web/acityscape_api/services/sun_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import aiohttp
77

88
measured_latency_in_sec = [0.399203, 0.7046, 0.422959, 0.741911, 0.404674]
9-
use_cached_data = False
9+
use_cached_data = True
1010

1111

1212
async def for_today(latitude: float, longitude: float) -> dict:

src/11-cython/perf/compute_threaded.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def main():
2727
dt = datetime.datetime.now() - t0
2828
print("Done in {:,.2f} sec. (factor: {:,.2f}x)".format(
2929
dt.total_seconds(),
30-
8.54/dt.total_seconds())
30+
8.54 / dt.total_seconds())
3131
)
3232

3333

@@ -41,3 +41,29 @@ def do_math(start=0, num=10):
4141

4242
if __name__ == '__main__':
4343
main()
44+
45+
import logging
46+
import threading
47+
import time
48+
49+
50+
def thread_function(name):
51+
logging.info("Thread %s: starting", name)
52+
time.sleep(2)
53+
logging.info("Thread %s: finishing", name)
54+
55+
56+
def main():
57+
format = "%(asctime)s: %(message)s"
58+
logging.basicConfig(format=format, level=logging.INFO,
59+
datefmt="%H:%M:%S")
60+
61+
logging.info("Main : before creating thread")
62+
x = threading.Thread(target=thread_function, args=(1,))
63+
logging.info("Main : before running thread")
64+
x.start()
65+
logging.info("Main : wait for the thread to finish")
66+
x.join()
67+
logging.info("Main : all done")
68+
69+
main()

src/11-cython/perf/math_core.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ from libc.math cimport sqrt
22

33
import cython
44

5+
56
def do_math(start: cython.float = 0, num: cython.float = 10):
67
pos: cython.float = start
78
k_sq: cython.float = 1000 * 1000

0 commit comments

Comments
 (0)