|
| 1 | +""" |
| 2 | +## Decorators |
| 3 | +
|
| 4 | +Documentation: https://thread.ngjx.org |
| 5 | +""" |
| 6 | + |
| 7 | +from functools import wraps |
| 8 | +from .thread import Thread |
| 9 | + |
| 10 | +from ._types import Overflow_In, Data_Out, Data_In |
| 11 | +from typing import Callable, Mapping, Sequence, Optional, Union, overload |
| 12 | +from typing_extensions import ParamSpec, TypeVar |
| 13 | + |
| 14 | + |
| 15 | +T = TypeVar('T') |
| 16 | +P = ParamSpec('P') |
| 17 | +TargetFunction = Callable[P, Data_Out] |
| 18 | +NoParamReturn = Callable[P, Thread] |
| 19 | +WithParamReturn = Callable[[TargetFunction], NoParamReturn] |
| 20 | +FullParamReturn = Callable[P, Thread] |
| 21 | +WrappedWithParamReturn = Callable[[TargetFunction], WithParamReturn] |
| 22 | + |
| 23 | + |
| 24 | +@overload |
| 25 | +def threaded(__function: TargetFunction) -> NoParamReturn: ... |
| 26 | + |
| 27 | +@overload |
| 28 | +def threaded( |
| 29 | + *, |
| 30 | + args: Sequence[Data_In] = (), |
| 31 | + kwargs: Mapping[str, Data_In] = {}, |
| 32 | + ignore_errors: Sequence[type[Exception]] = (), |
| 33 | + suppress_errors: bool = False, |
| 34 | + **overflow_kwargs: Overflow_In |
| 35 | +) -> WithParamReturn: ... |
| 36 | + |
| 37 | +@overload |
| 38 | +def threaded( |
| 39 | + __function: Callable[P, Data_Out], |
| 40 | + *, |
| 41 | + args: Sequence[Data_In] = (), |
| 42 | + kwargs: Mapping[str, Data_In] = {}, |
| 43 | + ignore_errors: Sequence[type[Exception]] = (), |
| 44 | + suppress_errors: bool = False, |
| 45 | + **overflow_kwargs: Overflow_In |
| 46 | +) -> FullParamReturn: ... |
| 47 | + |
| 48 | + |
| 49 | +def threaded( |
| 50 | + __function: Optional[TargetFunction] = None, |
| 51 | + *, |
| 52 | + args: Sequence[Data_In] = (), |
| 53 | + kwargs: Mapping[str, Data_In] = {}, |
| 54 | + ignore_errors: Sequence[type[Exception]] = (), |
| 55 | + suppress_errors: bool = False, |
| 56 | + **overflow_kwargs: Overflow_In |
| 57 | +) -> Union[NoParamReturn, WithParamReturn, FullParamReturn]: |
| 58 | + """ |
| 59 | + Decorate a function to run it in a thread |
| 60 | +
|
| 61 | + Parameters |
| 62 | + ---------- |
| 63 | + :param __function: The function to run in a thread |
| 64 | + :param args: Keyword-Only arguments to pass into `thread.Thread` |
| 65 | + :param kwargs: Keyword-Only keyword arguments to pass into `thread.Thread` |
| 66 | + :param ignore_errors: Keyword-Only arguments to pass into `thread.Thread` |
| 67 | + :param suppress_errors: Keyword-Only arguments to pass into `thread.Thread` |
| 68 | + :param **: Keyword-Only arguments to pass into `thread.Thread` |
| 69 | +
|
| 70 | + Returns |
| 71 | + ------- |
| 72 | + :return decorator: |
| 73 | +
|
| 74 | + Use Case |
| 75 | + -------- |
| 76 | + Now whenever `myfunction` is invoked, it will be executed in a thread and the `Thread` object will be returned |
| 77 | +
|
| 78 | + >>> @thread.threaded |
| 79 | + >>> def myfunction(*args, **kwargs): ... |
| 80 | +
|
| 81 | + >>> myJob = myfunction(1, 2) |
| 82 | + >>> type(myjob) |
| 83 | + > Thread |
| 84 | +
|
| 85 | + You can also pass keyword arguments to change the thread behaviour, it otherwise follows the defaults of `thread.Thread` |
| 86 | + >>> @thread.threaded(daemon = True) |
| 87 | + >>> def myfunction(): ... |
| 88 | +
|
| 89 | + Args will be ordered infront of function-parsed args parsed into `thread.Thread.args` |
| 90 | + >>> @thread.threaded(args = (1)) |
| 91 | + >>> def myfunction(*args): |
| 92 | + >>> print(args) |
| 93 | + >>> |
| 94 | + >>> myfunction(4, 6).get_return_value() |
| 95 | + 1, 4, 6 |
| 96 | + """ |
| 97 | + |
| 98 | + if not callable(__function): |
| 99 | + def wrapper(func: TargetFunction) -> FullParamReturn: |
| 100 | + return threaded( |
| 101 | + func, |
| 102 | + args = args, |
| 103 | + kwargs = kwargs, |
| 104 | + ignore_errors = ignore_errors, |
| 105 | + suppress_errors = suppress_errors, |
| 106 | + **overflow_kwargs |
| 107 | + ) |
| 108 | + return wrapper |
| 109 | + |
| 110 | + overflow_kwargs.update({ |
| 111 | + 'ignore_errors': ignore_errors, |
| 112 | + 'suppress_errors': suppress_errors |
| 113 | + }) |
| 114 | + |
| 115 | + kwargs = dict(kwargs) |
| 116 | + |
| 117 | + @wraps(__function) |
| 118 | + def wrapped(*parsed_args: P.args, **parsed_kwargs: P.kwargs) -> Thread: |
| 119 | + kwargs.update(parsed_kwargs) |
| 120 | + |
| 121 | + processed_args = ( *args, *parsed_args ) |
| 122 | + processed_kwargs = { i:v for i,v in parsed_kwargs.items() if i not in ['args', 'kwargs'] } |
| 123 | + |
| 124 | + job = Thread( |
| 125 | + target = __function, |
| 126 | + args = processed_args, |
| 127 | + kwargs = processed_kwargs, |
| 128 | + **overflow_kwargs |
| 129 | + ) |
| 130 | + job.start() |
| 131 | + return job |
| 132 | + |
| 133 | + return wrapped |
| 134 | + |
| 135 | + |
0 commit comments