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
21import logging
22from queue import Queue
23from threading import Thread
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._start_event_loop()
32
33    def submit(self, coroutine, callback=None):
34        # add item to the task queue
35        self._task_add()
36        future = asyncio.run_coroutine_threadsafe(coro=coroutine, loop=self.event_loop)
37        future.add_done_callback(callback) if callback else None
38        # remove item from the task queue after the task is done
39        future.add_done_callback(self._task_done)
40
41    def await_taskpool(self):
42        asyncio.run_coroutine_threadsafe(coro=self._stop_loop(), loop=self.event_loop)
43
44    def task_join(self):
45        self.task_queue.join()
46
47    def _task_add(self, item=1):
48        self.task_queue.put(item)
49
50    def _task_done(self, future):
51        # clear the task queue and stop the task pool once an exception occurs in the task
52        if future.exception():
53            logging.error(f'future.exception: {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        event_loop_thread = Thread(target=self._set_and_run_loop, args=(loop,))
78        event_loop_thread.setDaemon(True)
79        event_loop_thread.setName('event_loop_thread')
80        event_loop_thread.start()
81