-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.py
41 lines (28 loc) · 1.18 KB
/
pool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# pylint: disable=missing-module-docstring
#
# Copyright (C) 2020-2021 by UsergeTeam@Github, < https://github.com/UsergeTeam >.
#
# This file is part of < https://github.com/UsergeTeam/Userge > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/UsergeTeam/Userge/blob/master/LICENSE >
#
# All rights reserved.
__all__ = ['submit_thread', 'run_in_thread']
import asyncio
from typing import Any, Callable
from concurrent.futures import ThreadPoolExecutor, Future
from functools import wraps, partial
from motor.frameworks.asyncio import _EXECUTOR
def submit_thread(func: Callable[[Any], Any], *args: Any, **kwargs: Any) -> Future:
return _EXECUTOR.submit(func, *args, **kwargs)
def run_in_thread(func: Callable[[Any], Any]) -> Callable[[Any], Any]:
@wraps(func)
async def wrapper(*args: Any, **kwargs: Any) -> Any:
loop = asyncio.get_running_loop()
return await loop.run_in_executor(_EXECUTOR, partial(func, *args, **kwargs))
return wrapper
def _get() -> ThreadPoolExecutor:
return _EXECUTOR
def _stop():
_EXECUTOR.shutdown()
_LOG.info(_LOG_STR, f"Stopped Pool : {_EXECUTOR._max_workers} Workers")