Question: Python - Please help me with ending this banking program. I am not sure even where to start? row 100? I feel that it should

Python - Please help me with ending this banking program. I am not sure even where to start? row 100? I feel that it should be right before the def wait_outside_bank function, because that is what was created in Part II. And how do I even create the list of Teller object, where the number should be the max_tellers? I am so confused. And creating the list that target the teller_job function, using the thread initializer ?!?! Please help. You can see my attempts below.

Python - Please help me with ending this banking program. I amnot sure even where to start? row 100? I feel that itshould be right before the def wait_outside_bank function, because that is whatwas created in Part II. And how do I even create theSCRIPT:

from threading import Semaphore, Thread, Lock from queue import Queue, Empty from random import randint from time import sleep from os import system, name

max_customers_in_bank = 10 # maximum number of customers that can be in the bank at one time max_customers = 30 # number of customers that will go to the bank today max_tellers = 3 # number of tellers working today teller_timeout = 10 # longest time that a teller will wait for new customers

class Customer(): ''' Customer objects that each has name attribute''' def __init__(self, name): self.name = name

def __str__(self): return f"{self.name}"

class Teller(): ''' Teller objects that each has name attribute''' def __init__(self, name): self.name = name

def __str__(self): return f"{self.name}"

def bankprint(lock, msg): ''' Print commands Args: lock: Lock() msg: string Returns: None ''' lock.acquire() try: print(msg) finally: lock.release()

def wait_outside_bank(customer, guard, teller_line, printlock): ''' Thread function for Customer object Args: customer: Customer object guard: Semaphore teller_line: Queue printlock: Lock() Returns: None ''' bankprint(printlock,f"{customer} is waiting outside the bank") try: bankprint(printlock,f" Security guard letting {customer} into the bank") guard.acquire() bankprint(printlock, f"(C) {customer} is trying to get into line") teller_line.put(customer) #guard.release() except Exception as Error: print("Cannot put {customer} into line " +str(Error))

def teller_job(teller, guard, teller_line, printlock): ''' Thread method for Teller object Args: teller: Teller object guard: Semaphore teller_line: Queue printlock: Lock() Returns: None ''' bankprint(printlock, f"[T] {teller} has started working") while True: try: #guard.acquire() customer = teller_line.get(timeout=teller_timeout) bankprint(printlock, f"[T] {teller} is now helping {customer}") sleep(randint(1,4)) bankprint(printlock, f"[T] {teller} is done helping {customer}") bankprint(printlock, f" Security is letting {customer} out of the bank") guard.release() except Empty: bankprint(printlock, f"[T] No one is in line, {teller} is going on a break") break

if __name__ == '__main__': printlock = Lock() teller_line = Queue(maxsize=max_customers_in_bank) guard = Semaphore(max_customers_in_bank)

bankprint(printlock, " Security guard starting their shift") bankprint(printlock, "*B* Bank open")

# create list of Customer objects and pass to thread method customers = [Customer("Customer " +str(i)) for i in range(1, max_customers+1)] for i in range(0, len(customers)): customer_thread = Thread(name=f"customers[i]", target = wait_outside_bank, args =(customers[i],guard, teller_line, printlock)) customer_thread.start()

sleep(5) bankprint(printlock, "*B* Tellers started working")

# create a list of Teller objects and pass to thread method in another list tellers = [Teller("Teller "+str(i)) for i in range(1,max_tellers+1)] tellers_list = [Thread(name=f"tellers[i]", target = teller_job, args = (tellers[i], guard, teller_line, printlock)) for i in range(0,len(tellers))] for teller in tellers_list: teller.start() for teller in tellers_list: teller.join()

bankprint(printlock, "*B* Bank closed")

Back in __main__ it is time to get the tellers to work. So, immediately after the 5 second sleep completed in Part II, do the following: Print a bank message indicating the tellers are going to start working now Create a list of Teller objects (the number should be max_tellers) Create a list of teller threads that target the teller_job method using the following arguments to the Thread initializer method: o target = teller_job args = (one of the teller objects, guard, teller_line, printlock) Launch all the teller threads Wait for all threads to complete Print a bank message indicating the bank is closed and let the program end from threading import Semaphore, Thread, Lock from queue import Queue, Empty from random import randint from time import sleep from os import system, name max_customers_in_bank = 10 # maximum number of customers that can be in the bank at one time max_customers = 30 # number of customers that will go to the bank today max_tellers = 3 # number of tellers working today teller_timeout = 10 # longest time that a teller will wait for new customers class Customer(): "" Customer objects that each has name attribute'' def __init__(self, name): self.name = name def _str_(self): return f"{self.name}" 8 9 10 11 A 12 13 14 15 16 17 18 19 20 21 A 22 23 24 25 26 27 28 29 30 A 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 class Teller(): Teller objects that each has name attribute' def _init__(self, name): self.name = name def_str_(self): return f"{self.name}" def bankprint(lock, msg): Print commands Args: Lock: Lock() msg: string Returns: None lock.acquire() try: print(msg) finally: lock release) def wait_outside_bank(customer, guard, teller_line, printlock): Thread function for Customer object Args: customer: Customer object guard: Semaphore teller_line: Queue printlock: Lock() Returns: None 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 bankprint(printlock,f"{customer} is waiting outside the bank") try: bankprint(printlock,f" Security guard letting {customer} into the bank") guard.acquire() bankprint(printlock, f"(C) {customer} is trying to get into Line") teller_line.put(customer) #guard.release) except Exception as Error: print("Cannot put {customer} into Line " +str(Error)) def teller_job(teller, guard, teller_line, printlock): Thread method for Teller object Args: teller: Teller object guard: Semaphore teller_line: Queue printlock: Lock() Returns: None 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 bankprint (printlock, f"[T] {teller} has started working") while True: try: #guard.acquire() customer = teller_line.get(timeout=teller_timeout) bankprint(printlock, f"[T] {teller} is now helping {customer}") sleep (randint(1,4)) bankprint(printlock, f"[T] {teller} is done helping {customer}") bankprint(printlock, f" Security is Letting {customer} out of the bank") guard.release) except Empty: bankprint(printlock, f"[T] No one is in line, {teller} is going on a break") break if name _main__': printlock = Lock() teller_line = Queue (maxsize=max_customers_in_bank) guard = Semaphore (max_customers in_bank) bankprint(printlock, " Security guard starting their shift") bankprint(printlock, "*B* Bank open") # create list of Customer objects and pass to thread method customers = [Customer("Customer " +str(i)) for i in range(1, max_customers+1)] for i in range(0, len(customers)): customer_thread = Thread(name=f"customers[i]", target = wait_outside_bank, args =(customers[i], guard, teller_line, printlock)) customer_thread.start() sleep(5) bankprint(printlock, "*B* Tellers started working") 106 107 108 109 A 110 111 112 113 114 115 116 117 A 118 119 120 121 122 123 124 # create a list of Teller objects and pass to thread method in another list tellers = [Teller("Teller "+str(i)) for i in range(1,max_tellers+1)] tellers_list = [Thread(name=f"tellers[i]", target = teller_job, args = (tellers[i], guard, teller_line, printlock)) for i in range(0, len(tellers))] for teller in tellers_list: teller.start() for teller in tellers_list: teller.join() bankprint (printlock, "*B* Bank closed") Back in __main__ it is time to get the tellers to work. So, immediately after the 5 second sleep completed in Part II, do the following: Print a bank message indicating the tellers are going to start working now Create a list of Teller objects (the number should be max_tellers) Create a list of teller threads that target the teller_job method using the following arguments to the Thread initializer method: o target = teller_job args = (one of the teller objects, guard, teller_line, printlock) Launch all the teller threads Wait for all threads to complete Print a bank message indicating the bank is closed and let the program end from threading import Semaphore, Thread, Lock from queue import Queue, Empty from random import randint from time import sleep from os import system, name max_customers_in_bank = 10 # maximum number of customers that can be in the bank at one time max_customers = 30 # number of customers that will go to the bank today max_tellers = 3 # number of tellers working today teller_timeout = 10 # longest time that a teller will wait for new customers class Customer(): "" Customer objects that each has name attribute'' def __init__(self, name): self.name = name def _str_(self): return f"{self.name}" 8 9 10 11 A 12 13 14 15 16 17 18 19 20 21 A 22 23 24 25 26 27 28 29 30 A 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 class Teller(): Teller objects that each has name attribute' def _init__(self, name): self.name = name def_str_(self): return f"{self.name}" def bankprint(lock, msg): Print commands Args: Lock: Lock() msg: string Returns: None lock.acquire() try: print(msg) finally: lock release) def wait_outside_bank(customer, guard, teller_line, printlock): Thread function for Customer object Args: customer: Customer object guard: Semaphore teller_line: Queue printlock: Lock() Returns: None 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 bankprint(printlock,f"{customer} is waiting outside the bank") try: bankprint(printlock,f" Security guard letting {customer} into the bank") guard.acquire() bankprint(printlock, f"(C) {customer} is trying to get into Line") teller_line.put(customer) #guard.release) except Exception as Error: print("Cannot put {customer} into Line " +str(Error)) def teller_job(teller, guard, teller_line, printlock): Thread method for Teller object Args: teller: Teller object guard: Semaphore teller_line: Queue printlock: Lock() Returns: None 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 bankprint (printlock, f"[T] {teller} has started working") while True: try: #guard.acquire() customer = teller_line.get(timeout=teller_timeout) bankprint(printlock, f"[T] {teller} is now helping {customer}") sleep (randint(1,4)) bankprint(printlock, f"[T] {teller} is done helping {customer}") bankprint(printlock, f" Security is Letting {customer} out of the bank") guard.release) except Empty: bankprint(printlock, f"[T] No one is in line, {teller} is going on a break") break if name _main__': printlock = Lock() teller_line = Queue (maxsize=max_customers_in_bank) guard = Semaphore (max_customers in_bank) bankprint(printlock, " Security guard starting their shift") bankprint(printlock, "*B* Bank open") # create list of Customer objects and pass to thread method customers = [Customer("Customer " +str(i)) for i in range(1, max_customers+1)] for i in range(0, len(customers)): customer_thread = Thread(name=f"customers[i]", target = wait_outside_bank, args =(customers[i], guard, teller_line, printlock)) customer_thread.start() sleep(5) bankprint(printlock, "*B* Tellers started working") 106 107 108 109 A 110 111 112 113 114 115 116 117 A 118 119 120 121 122 123 124 # create a list of Teller objects and pass to thread method in another list tellers = [Teller("Teller "+str(i)) for i in range(1,max_tellers+1)] tellers_list = [Thread(name=f"tellers[i]", target = teller_job, args = (tellers[i], guard, teller_line, printlock)) for i in range(0, len(tellers))] for teller in tellers_list: teller.start() for teller in tellers_list: teller.join() bankprint (printlock, "*B* Bank closed")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!