99customer_queue = []
1010
1111
12+ def now ():
13+ return time .strftime ("%H:%M:%S" )
14+
15+
1216def serve_customers ():
1317 while True :
1418 with customer_available_condition :
1519 # Wait for a customer to arrive
1620 while not customer_queue :
17- print (f"{ int ( time . time () )} : Teller is waiting for a customer." )
21+ print (f"{ now ( )} : Teller is waiting for a customer." )
1822 customer_available_condition .wait ()
1923
2024 # Serve the customer
2125 customer = customer_queue .pop (0 )
22- print (f"{ int ( time . time () )} : Teller is serving { customer } ." )
26+ print (f"{ now ( )} : Teller is serving { customer } ." )
2327
2428 # Simulate the time taken to serve the customer
25- time .sleep (random .randint (1 , 3 ))
26- print (f"{ int ( time . time () )} : Teller has finished serving { customer } ." )
29+ time .sleep (random .randint (1 , 5 ))
30+ print (f"{ now ( )} : Teller has finished serving { customer } ." )
2731
2832
2933def add_customer_to_queue (name ):
3034 with customer_available_condition :
31- print (f"{ int ( time . time () )} : { name } has arrived at the bank." )
35+ print (f"{ now ( )} : { name } has arrived at the bank." )
3236 customer_queue .append (name )
3337
3438 customer_available_condition .notify ()
@@ -43,11 +47,8 @@ def add_customer_to_queue(name):
4347]
4448
4549with ThreadPoolExecutor (max_workers = 6 ) as executor :
46-
4750 teller_thread = executor .submit (serve_customers )
48-
4951 for name in customer_names :
5052 # Simulate customers arriving at random intervals
51- time .sleep (random .randint (2 , 5 ))
52-
53+ time .sleep (random .randint (1 , 3 ))
5354 executor .submit (add_customer_to_queue , name )
0 commit comments