class ProducerProcess: __Queue_Name = "test_queue" def __init__(self): self.__condition_opt = ConditionOperator() self.__async_condition_opt = ConditionAsyncOperator() self.__queue_opt = QueueOperator() 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() 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()
class ConsumerProcess: __Queue_Name = "test_queue" def __init__(self): self.__condition_opt = ConditionOperator() self.__async_condition_opt = ConditionAsyncOperator() self.__queue_opt = QueueOperator() 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.")
async def sql_process_with_semaphore(self): import multiprocessing as mp self._logger.debug("Running all things with Bounded Semaphore.") # sql_tasks = self.get_all_sql_tasks() sql_tasks = QueueOperator.get_queue_with_name(name="test_sql_task") self._logger.debug(f"SQL tasks: {sql_tasks} - {mp.current_process()}") one_sql_query = await sql_tasks.get() self._logger.debug(f"SQL tasks query: {one_sql_query} - {mp.current_process()}") self.database_opt.execute(one_sql_query) data = self.database_opt.fetch_all() return data
def sql_process_with_lock(self): import multiprocessing as mp self._logger.debug("Running all things with Lock.") # sql_tasks = self.get_all_sql_tasks() sql_tasks = QueueOperator.get_queue_with_name(name="test_sql_task") self._logger.debug(f"SQL tasks: {sql_tasks} - {mp.current_process()}") one_sql_query = sql_tasks.get() self._logger.debug( f"SQL tasks query: {one_sql_query} - {mp.current_process()}") self.execute(one_sql_query) data = self.fetch_all() return data
def __init__(self): self.__condition_opt = ConditionOperator() self.__async_condition_opt = ConditionAsyncOperator() self.__queue_opt = QueueOperator()