1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2024 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17Description: A task pool that can execute tasks asynchronously. 18""" 19 20import asyncio 21from queue import Queue 22from threading import Thread, current_thread 23from time import time 24 25 26class TaskPool(object): 27 def __init__(self): 28 self.task_queue = Queue() 29 self.event_loop = None 30 self.task_exception = None 31 self.event_loop_thread = None 32 self._start_event_loop() 33 34 def submit(self, coroutine, callback=None): 35 # add item to the task queue 36 self._task_add() 37 future = asyncio.run_coroutine_threadsafe(coro=coroutine, loop=self.event_loop) 38 future.add_done_callback(callback) if callback else None 39 # remove item from the task queue after the task is done 40 future.add_done_callback(self._task_done) 41 42 def await_taskpool(self): 43 asyncio.run_coroutine_threadsafe(coro=self._stop_loop(), loop=self.event_loop) 44 45 def task_join(self): 46 self.task_queue.join() 47 48 def _task_add(self, item=1): 49 self.task_queue.put(item) 50 51 def _task_done(self, future): 52 # clear the task queue and stop the task pool once an exception occurs in the task 53 if future.exception(): 54 while not self.task_queue.empty(): 55 self.task_queue.get() 56 self.task_queue.task_done() 57 self.task_exception = future.exception() 58 return 59 self.task_queue.get() 60 self.task_queue.task_done() 61 62 def _set_and_run_loop(self, loop): 63 self.event_loop = loop 64 asyncio.set_event_loop(loop) 65 loop.run_forever() 66 67 async def _stop_loop(self, interval=1): 68 # wait for all tasks in the event loop is done, then we can close the loop 69 while True: 70 if self.task_queue.empty(): 71 self.event_loop.stop() 72 return 73 await asyncio.sleep(interval) 74 75 def _start_event_loop(self): 76 loop = asyncio.new_event_loop() 77 self.event_loop_thread = Thread(target=self._set_and_run_loop, args=(loop,)) 78 self.event_loop_thread.daemon = True 79 # Specifies the thread name to be able to save log of thread to the module_run.log file 80 self.event_loop_thread.name = current_thread().name + "-" + str(time()).replace('.', '')[-5:] 81 self.event_loop_thread.start() 82