def receive_process(self, *args): print("[Consumer] args: ", args) test_queue = self.__queue_opt.get_queue_with_name( name=self.__Queue_Name) print( f"[Consumer] It detects the message which be produced by ProducerThread." ) while True: # # # # 1. Method # self.__condition_opt.acquire() # time.sleep(1) # print("[Consumer] ConsumerThread waiting ...") # self.__condition_opt.wait() # __sleep_time = test_queue.get() # print("[Consumer] ConsumerThread re-start.") # print(f"[Consumer] ProducerThread sleep {__sleep_time} seconds.") # self.__condition_opt.release() # # # # 2. Method __condition = self.__condition_opt with __condition: sleep(1) print("[Consumer] ConsumerThread waiting ...") self.__condition_opt.wait() __sleep_time = test_queue.get() print("[Consumer] ConsumerThread re-start.") print( f"[Consumer] ProducerThread sleep {__sleep_time} seconds.")
def target_function(self, *args, **kwargs) -> str: print("This is ExampleParallelClient.target_function in process.") sleep(3) print("This is target function args: ", args) print("This is target function kwargs: ", kwargs) # raise Exception("Test for error") return "You are 87."
def target_function(self, *args, **kwargs) -> str: print("This is ExampleTargetFunction.target_function.") sleep(3) print("This is target function args: ", args) print("This is target function kwargs: ", kwargs) # raise Exception("Test for error") return "You are 87."
def target_fail_function(self, *args, **kwargs) -> None: print("This is ExampleParallelClient.target_function.") print("This is target function args: ", args) print("This is target function kwargs: ", kwargs) print("It will raise exception after 3 seconds ...") multirunnable.sleep(3) raise Exception("Test for error")
def wake_other_process(self, *args): print("[WakeupProcess] args: ", args) print(f"[WakeupProcess] It will keep producing something useless message.") while True: __sleep_time = random.randrange(1, 10) print(f"[WakeupProcess] It will sleep for {__sleep_time} seconds.") sleep(__sleep_time) self.__event_opt.set()
def go_sleep(self, *args): print("[SleepProcess] args: ", args) print(f"[SleepProcess] It detects the message which be produced by ProducerThread.") while True: sleep(1) print("[SleepProcess] ConsumerThread waiting ...") self.__event_opt.wait() print("[SleepProcess] ConsumerThread wait up.") self.__event_opt.clear()
def target_function(self, *args, **kwargs) -> str: print("This is ExampleTargetFunction.target_function.") sleep(3) print("This is target function args: ", args) print("This is target function kwargs: ", kwargs) sleep(2) self.__rlock.acquire() print("Lock Acquire 1 time") sleep(2) self.__rlock.acquire() print("Lock Acquire 2 time") sleep(2) self.__rlock.release() print("Lock Release 1 time") sleep(2) self.__rlock.release() print("Lock Release 2 time") return "You are 87."
def send_process(self, *args): print("[Producer] args: ", args) test_queue = self.__queue_opt.get_queue_with_name( name=self.__Queue_Name) print(f"[Producer] It will keep producing something useless message.") while True: __sleep_time = random.randrange(1, 10) print(f"[Producer] It will sleep for {__sleep_time} seconds.") test_queue.put(__sleep_time) sleep(__sleep_time) # # # # # 1. Method # self.__condition_opt.acquire() # self.__condition_opt.notify_all() # self.__condition_opt.release() # # # # # 2. Method __condition = self.__condition_opt with __condition: self.__condition_opt.notify_all()
def lock_function(self): print("This is testing process with Lock and sleep for 3 seconds.") sleep(3) print("Wake up and raise an exception ...") raise RuntimeError("Test for error")