Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python - I need help with get the tellers to service the customers. I need to create a thread method, teller_job(), that will allow customers

Python - I need help with get the tellers to service the customers. I need to create a thread method, teller_job(), that will allow customers to be served by tellers. However, I am stuck. How do I switch to that the teller is now the first one? And the infinite loop is so confusing....and the except block should handle a queue?!?! Please help, you can see my attempts below.image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

SCRIPT:

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

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(): def __init__(self, name): self.name = name def __str__(self): return f"'{self.name}'"

class Teller(): def __init__(self, name): self.name = name def __str__(self): return f"'{self.name}'"

def bank_print(lock, msg): with lock: print(msg) def wait_outside_bank(customer, guard, teller_line, printlock): bank_print(printlock, f"(C) {customer} waiting outside bank") guard.acquire() bank_print(printlock, f" Security guard letting {customer} inside bank") bank_print(printlock, f"(C) {customer} getting into line") teller_line.put(customer) def teller_job(teller, guard, teller_line, printlock): bank_print(printlock, f"[T] {teller} starting work") while True: try: c = teller_line.get(timeout=teller_timeout) bank_print(printlock, f"[T] {teller} is now helping {c}") sleep(randint(1,4)) bank_print(printlock, f"[T] {teller} done helping {c}") bank_print(printlock, f" Security guard is letting {c} out of the bank") guard.release() except Empty: bank_print(printlock, f"[T] Nobody is in line, {teller} going on break") break

if __name__ == "__main__": printlock = Lock() teller_line = Queue(maxsize=max_customers_in_bank) guard = Semaphore(max_customers_in_bank) bank_print(printlock, " Security guard starting their shift") bank_print(printlock, "*B* Bank open")

customer_jobs = [] for i in range(1, max_customers + 1): customer_name = 'Customer ' + str(i) customer = Customer(customer_name) c = Thread(target=wait_outside_bank, args=(customer, guard, teller_line, printlock)) c.start() customer_jobs.append(c)

sleep(5) # tellers has to wait for 5 sec to get to works bank_print(printlock, f"*B* Tellers starting work now") teller_jobs = [] for i in range(1, max_tellers+1): sleep(5) # each teller has to wait for 5 sec to get back to work for another customer teller_name = 'Teller ' + str(i) teller = Teller(teller_name) t = Thread(target=teller_job, args=(teller, guard, teller_line, printlock)) t.start() teller_jobs.append(t) for cj in customer_jobs: cj.join() for tj in teller_jobs: tj.join() bank_print(printlock, f"*B* Bank closed") def wait_outside_bank(customer, guard, teller_line, printlock): target = wait_outside_bank args = (customer, guard, teller_line, printlock) def teller_job(teller, guard, teller_line, printlock):

Now, you need to create a thread method, teller_job(), that will allow customers to be served by tellers: def teller_job(teller, guard, teller_line, printlock): Where: teller is a Teller object guard is the security guard semaphore teller_line is the Queue printlock is the Lock As you can see, the parameters are almost the same as wait_outside_bank(). The major difference is the first one which is now a teller since, unlike the other thread which was customer-centric, this one will be teller-centric. Print a teller message indicating that this teller has started work Create an infinite loop and put the rest of the code in it o Use a try block, and place the following code within it (see the except code later): Get a customer from the teller_line (use queue's get method() - provide teller_timeout for the timeout argument) Print a teller message indicating this teller is now helping this customer Sleep a random amount of time (1 - 4 seconds) to simulate the teller working with the customer Print a teller message indicating this teller is done helping this customer Print a security guard message indicating they are letting this customer out of the bank Release the semaphore from the guard object The except block should handle a Queue.Empty error (just Empty is OK due to the import) Print a teller message indicating that the teller is going on break and exit the loop 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 o 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 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(): def _init__(self, name): self.name = name def _str_(self): return f"'{self.name}"" 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 class Teller(): def _init__(self, name): self.name = name def str_(self): return f"'{self.name} '" def bank_print(lock, msg): with lock: print(msg) def wait_outside_bank(customer, guard, teller_line, printlock): bank_print (printlock, f"(C) {customer} waiting outside bank") guard.acquire() bank_print(printlock, f" Security guard letting {customer} inside bank") bank_print(printlock, f"(C) {customer} getting into Line") teller_line.put(customer) def teller_job(teller, guard, teller_line, printlock): bank_print (printlock, f"[T] {teller} starting work") while True: try: c = teller_line.get(timeout=teller_timeout) bank_print (printlock, f"[T] {teller} is now helping {c}") sleep(randint(1,4)) bank_print (printlock, f"[T] {teller} done helping {c}") bank_print (printlock, f" Security guard is Letting {c} out of the bank") guard.release) except Empty: bank_print (printlock, f"[T] Nobody is in line, {teller} going on break") break 46 47 48 49 50 51 52 53 54 55 56 57 58 59 if name == _main__": printlock = Lock() teller_line = Queue (maxsize=max_customers_in_bank) guard = Semaphore(max_customers_in_bank) bank_print (printlock, " Security guard starting their shift") bank_print(printlock, "*B* Bank open") GO customer_jobs = [] 60 61 62 63 64 65 = 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 for i in range(1, max_customers + 1): customer_name = *Customer ' + str(i) customer Customer (customer_name) c = Thread(target=wait_outside_bank, args=(customer, guard, teller_line, printlock)) c.start() customer_jobs.append(c) sleep(5) # tellers has to wait for 5 sec to get to works bank_print(printlock, f"*B* Tellers starting work now") teller_jobs for i in range(1, max_tellers+1): sleep(5) # each teller has to wait for 5 sec to get back to work for another customer teller_name = 'Teller ' + str(i) teller = Teller(teller_name) t = Thread(target=teller_job, args=(teller, guard, teller_line, printlock)) t.start() teller_jobs.append(t) for cj in customer_jobs: cj.join() for tj in teller_jobs: tj.join() bank_print(printlock, f"*B* Bank closed") def wait_outside_bank(customer, guard, teller_line, printlock): target = wait_outside_bank args = (customer, guard, teller_line, printlock) def teller_job(teller, guard, teller_line, printlock)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Implementing Ai And Machine Learning For Business Optimization

Authors: Robert K Wiley

1st Edition

B0CPQJW72N, 979-8870675855

More Books

Students also viewed these Databases questions