class ProducerProcess: __Queue_Name = "test_queue" def __init__(self): self.__condition_opt = ConditionOperator() self.__async_condition_opt = ConditionAsyncOperator() self.__queue_opt = QueueOperator() async def async_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: # # Method 1 # __sleep_time = random.randrange(1, 10) # print(f"[Producer] It will sleep for {__sleep_time} seconds.") # await test_queue.put(__sleep_time) # await asyncio.sleep(__sleep_time) # __condition = self.__async_condition_opt # await __condition.acquire() # __condition.notify_all() # __condition.release() # # Method 2 __sleep_time = random.randrange(1, 10) print(f"[Producer] It will sleep for {__sleep_time} seconds.") await test_queue.put(__sleep_time) await async_sleep(__sleep_time) __condition = self.__async_condition_opt async with __condition: self.__condition_opt.notify_all()
def __init__(self): self.__condition_opt = ConditionOperator() self.__async_condition_opt = ConditionAsyncOperator() self.__queue_opt = QueueOperator()
class ConsumerProcess: __Queue_Name = "test_queue" def __init__(self): self.__condition_opt = ConditionOperator() self.__async_condition_opt = ConditionAsyncOperator() self.__queue_opt = QueueOperator() 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.") async def async_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: __condition = self.__async_condition_opt # # Method 1 # await __condition.acquire() # await asyncio.sleep(1) # print("[Consumer] ConsumerThread waiting ...") # await __condition.wait() # __sleep_time = await test_queue.get() # print("[Consumer] ConsumerThread re-start.") # print(f"[Consumer] ProducerThread sleep {__sleep_time} seconds.") # __condition.release() # # Method 2 async with __condition: await async_sleep(1) print("[Consumer] ConsumerThread waiting ...") await __condition.wait() __sleep_time = await test_queue.get() print("[Consumer] ConsumerThread re-start.") print( f"[Consumer] ProducerThread sleep {__sleep_time} seconds.")