__all__ = ['submit_task', 'submit_thread', 'run_in_thread'] import asyncio from typing import Any, Callable, List from concurrent.futures import ThreadPoolExecutor, Future from functools import wraps, partial from naruto import logging, Config _WORKERS = Config.WORKERS _THREAD_POOL: ThreadPoolExecutor _ASYNC_QUEUE = asyncio.Queue() _TASKS: List[asyncio.Task] = [] _LOG = logging.getLogger(__name__) _LOG_STR = "<<<! |||| %s |||| !>>>" def submit_task(task: asyncio.coroutines.CoroWrapper) -> None: """ submit task to task pool """ _ASYNC_QUEUE.put_nowait(task) def submit_thread(func: Callable[[Any], Any], *args: Any, **kwargs: Any) -> Future: """ submit thread to thread pool """ return _THREAD_POOL.submit(func, *args, **kwargs) def run_in_thread(func: Callable[[Any], Any]) -> Callable[[Any], Any]: """ run in a thread """ @wraps(func)
def getLogger(name: str = '') -> logging.Logger: # pylint: disable=invalid-name """ This returns new logger object """ if not name: name = inspect.currentframe().f_back.f_globals['__name__'] _LOG.debug(_LOG_STR, f"Creating Logger => {name}") return logging.getLogger(name)