Skip to content

Commit 63f4d5d

Browse files
Updated thread.threaded implementation
1 parent 7d9e39d commit 63f4d5d

1 file changed

Lines changed: 44 additions & 11 deletions

File tree

src/thread/decorators.py

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,20 @@
44
Documentation: https://thread.ngjx.org
55
"""
66

7-
import inspect
8-
from functools import wraps, partial
9-
from abc import ABC, abstractmethod
7+
from functools import wraps
8+
from .thread import Thread
109

1110
from ._types import Overflow_In, Data_Out, Data_In
12-
from typing import Any, Callable, Mapping, Tuple, Sequence, Optional, Union, Protocol, Iterable, overload
11+
from typing import Any, Callable, Mapping, Sequence, Optional, Union, overload
1312
from typing_extensions import ParamSpec, TypeVar, Concatenate
1413

15-
from .thread import Thread
16-
from .exceptions import AbstractInvokationError, ArgumentValidationError
17-
1814

1915
T = TypeVar('T')
2016
P = ParamSpec('P')
2117
TargetFunction = Callable[P, Data_Out]
2218
NoParamReturn = Callable[P, Thread]
2319
WithParamReturn = Callable[[TargetFunction], NoParamReturn]
24-
FullParamReturn = Callable[P, Thread]
20+
FullParamReturn = Callable[Concatenate[TargetFunction, ...], Thread]
2521
WrappedWithParamReturn = Callable[[TargetFunction], WithParamReturn]
2622

2723

@@ -60,7 +56,43 @@ def threaded(
6056
**overflow_kwargs: Overflow_In
6157
) -> Union[NoParamReturn, WithParamReturn, FullParamReturn]:
6258
"""
63-
test 1
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
6496
"""
6597

6698
if not callable(__function):
@@ -84,11 +116,10 @@ def wrapper(func: TargetFunction) -> FullParamReturn:
84116

85117
@wraps(__function)
86118
def wrapped(*parsed_args: P.args, **parsed_kwargs: P.kwargs) -> Thread:
87-
"""test 2"""
88119
kwargs.update(parsed_kwargs)
89120

90121
processed_args = ( *args, *parsed_args )
91-
processed_kwargs = { i:v for i,v in kwargs.items() if i not in ['args', 'kwargs'] }
122+
processed_kwargs = { i:v for i,v in parsed_kwargs.items() if i not in ['args', 'kwargs'] }
92123

93124
job = Thread(
94125
target = __function,
@@ -100,3 +131,5 @@ def wrapped(*parsed_args: P.args, **parsed_kwargs: P.kwargs) -> Thread:
100131
return job
101132

102133
return wrapped
134+
135+

0 commit comments

Comments
 (0)