Mercurial
annotate asyncio_threads/bank_question/main.py @ 71:75de5903355c
Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Sun, 28 Dec 2025 20:34:22 -0800 |
| parents | 46daba6e3cf4 |
| children |
| rev | line source |
|---|---|
|
48
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
1 # # Bank Account |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
2 # |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
3 # You are given a simplified multi-threaded “bank account” implementation used by one of our infrastructure teams. The current code processes deposits concurrently but produces incorrect results. |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
4 # |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
5 # Task |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
6 # |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
7 # Identify the concurrency bugs in the implementation. |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
8 # |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
9 # Explain why these bugs occur. |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
10 # |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
11 # Fix the implementation without modifying the existing BankAccount class directly. |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
12 # |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
13 # You may use subclassing (Python) or embedding (Go-style composition), depending on the language you choose. |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
14 # |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
15 # Ensure your solution is thread-safe and produces the correct final balance (1200). |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
16 |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
17 from threading import Lock |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
18 from concurrent.futures import ThreadPoolExecutor |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
19 import time |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
20 |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
21 class BankAccount: |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
22 def __init__(self, balance): |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
23 self.balance = balance |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
24 self._lock = Lock() |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
25 |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
26 def deposit(self, amount): |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
27 while (self._lock.locked()): |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
28 pass |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
29 self._lock.acquire() |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
30 new_balance = self.balance + amount # read |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
31 time.sleep(0.1) # simulate delay |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
32 self.balance = new_balance # write |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
33 self._lock.release() |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
34 |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
35 |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
36 account = BankAccount(0) |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
37 |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
38 with ThreadPoolExecutor(max_workers=2) as executor: |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
39 futures = [ |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
40 executor.submit(account.deposit, 500), |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
41 executor.submit(account.deposit, 700), |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
42 ] |
|
46daba6e3cf4
Few python scrtips to show how to use asychio.
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
43 print(account.balance) |