Skip to content

Commit

Permalink
Final QA (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
gahjelle authored Oct 11, 2024
1 parent 2c30025 commit 98d865b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 34 deletions.
12 changes: 8 additions & 4 deletions thread-safety-locks/bank_barrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
teller_barrier = threading.Barrier(3)


def now():
return time.strftime("%H:%M:%S")


def prepare_for_work(name):
print(f"{int(time.time())}: {name} is preparing their counter.")
print(f"{now()}: {name} is preparing their counter.")

# Simulate the delay to prepare the counter
time.sleep(random.randint(1, 3))
print(f"{int(time.time())}: {name} has finished preparing.")
print(f"{now()}: {name} has finished preparing.")

# Wait for all tellers to finish preparing
teller_barrier.wait()
print(f"{int(time.time())}: {name} is now ready to serve customers.")
print(f"{now()}: {name} is now ready to serve customers.")


tellers = ["Teller 1", "Teller 2", "Teller 3"]
Expand All @@ -24,4 +28,4 @@ def prepare_for_work(name):
for teller_name in tellers:
executor.submit(prepare_for_work, teller_name)

print(f"{int(time.time())}: All tellers are ready to serve customers.")
print(f"{now()}: All tellers are ready to serve customers.")
19 changes: 10 additions & 9 deletions thread-safety-locks/bank_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@
customer_queue = []


def now():
return time.strftime("%H:%M:%S")


def serve_customers():
while True:
with customer_available_condition:
# Wait for a customer to arrive
while not customer_queue:
print(f"{int(time.time())}: Teller is waiting for a customer.")
print(f"{now()}: Teller is waiting for a customer.")
customer_available_condition.wait()

# Serve the customer
customer = customer_queue.pop(0)
print(f"{int(time.time())}: Teller is serving {customer}.")
print(f"{now()}: Teller is serving {customer}.")

# Simulate the time taken to serve the customer
time.sleep(random.randint(1, 3))
print(f"{int(time.time())}: Teller has finished serving {customer}.")
time.sleep(random.randint(1, 5))
print(f"{now()}: Teller has finished serving {customer}.")


def add_customer_to_queue(name):
with customer_available_condition:
print(f"{int(time.time())}: {name} has arrived at the bank.")
print(f"{now()}: {name} has arrived at the bank.")
customer_queue.append(name)

customer_available_condition.notify()
Expand All @@ -43,11 +47,8 @@ def add_customer_to_queue(name):
]

with ThreadPoolExecutor(max_workers=6) as executor:

teller_thread = executor.submit(serve_customers)

for name in customer_names:
# Simulate customers arriving at random intervals
time.sleep(random.randint(2, 5))

time.sleep(random.randint(1, 3))
executor.submit(add_customer_to_queue, name)
11 changes: 3 additions & 8 deletions thread-safety-locks/bank_deadlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self):
def deposit(self, amount):
print(
f"Thread {threading.current_thread().name} waiting "
f"to acquire lock for deposit()"
"to acquire lock for deposit()"
)
with self.lock:
print(
Expand All @@ -24,7 +24,7 @@ def deposit(self, amount):
def _update_balance(self, amount):
print(
f"Thread {threading.current_thread().name} waiting to acquire "
f"lock for _update_balance()"
"lock for _update_balance()"
)
with self.lock: # This will cause a deadlock
print(
Expand All @@ -36,16 +36,11 @@ def _update_balance(self, amount):

account = BankAccount()


def make_deposit():
account.deposit(100)


with ThreadPoolExecutor(
max_workers=3, thread_name_prefix="Worker"
) as executor:
for _ in range(3):
executor.submit(make_deposit)
executor.submit(account.deposit, 100)


print(f"Final balance: {account.balance}")
2 changes: 1 addition & 1 deletion thread-safety-locks/bank_multithreaded_withdrawal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def withdraw(self, amount):
time.sleep(0.1) # Simulate a delay
self.balance = new_balance
else:
raise Exception("Insufficient balance")
raise ValueError("Insufficient balance")


account = BankAccount()
Expand Down
7 changes: 1 addition & 6 deletions thread-safety-locks/bank_rlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,11 @@ def _update_balance(self, amount):

account = BankAccount()


def make_deposit():
account.deposit(100)


with ThreadPoolExecutor(
max_workers=3, thread_name_prefix="Worker"
) as executor:
for _ in range(3):
executor.submit(make_deposit)
executor.submit(account.deposit, 100)


print(f"Final balance: {account.balance}")
12 changes: 8 additions & 4 deletions thread-safety-locks/bank_semaphore.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
teller_semaphore = threading.Semaphore(2)


def now():
return time.strftime("%H:%M:%S")


def serve_customer(name):
print(f"{int(time.time())}: {name} is waiting for a teller.")
print(f"{now()}: {name} is waiting for a teller.")
with teller_semaphore:
print(f"{int(time.time())}: {name} is being served by a teller.")
print(f"{now()}: {name} is being served by a teller.")
# Simulate the time taken for the teller to serve the customer
time.sleep(random.randint(1, 3))
print(f"{int(time.time())}: {name} is done being served.")
print(f"{now()}: {name} is done being served.")


customers = [
Expand All @@ -29,4 +33,4 @@ def serve_customer(name):
thread = executor.submit(serve_customer, customer_name)


print("All customers have been served.")
print(f"{now()}: All customers have been served.")
3 changes: 1 addition & 2 deletions thread-safety-locks/bank_thread_safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def withdraw(self, amount):
time.sleep(0.1) # Simulate a delay
self.balance = new_balance
else:
raise Exception("Insufficient balance")
raise ValueError("Insufficient balance")

def deposit(self, amount):
with self.account_lock:
Expand All @@ -29,7 +29,6 @@ def deposit(self, amount):
account = BankAccount(1000)

with ThreadPoolExecutor(max_workers=3) as executor:

executor.submit(account.withdraw, 700)
executor.submit(account.deposit, 1000)
executor.submit(account.withdraw, 300)
Expand Down

0 comments on commit 98d865b

Please sign in to comment.