1919
2020
2121class task :
22- """Decorator class to initialize a `Pipeline` consisting of one task, from a function or callable .
22+ """Decorator class to initialize a `Pipeline` consisting of one task.
2323
2424 Args:
25- func (callable): A positional-only param defining the task function
25+ func (callable): A positional-only param defining the task function (can be omitted when using `@task`)
26+ branch (bool): Allows the task to submit multiple outputs
2627 join (bool): Allows the task to take all previous results as input, instead of single results
2728 workers (int): Defines the number of workers to run the task
2829 throttle (int): Limits the number of results the task is able to produce when all consumers are busy
@@ -32,7 +33,7 @@ class task:
3233 Returns:
3334 Pipeline: A `Pipeline` instance consisting of one task.
3435
35- Example:
36+ Example:
3637 ```python
3738 def f(x: int):
3839 return x + 1
@@ -46,6 +47,7 @@ def __new__(
4647 func : None = None ,
4748 / ,
4849 * ,
50+ branch : bool = False ,
4951 join : bool = False ,
5052 workers : int = 1 ,
5153 throttle : int = 0 ,
@@ -55,21 +57,23 @@ def __new__(
5557 @t .overload
5658 def __new__ (
5759 cls ,
58- func : t .Callable [_P , t .Awaitable [_R ]],
60+ func : t .Callable [_P , t .Union [ t . Awaitable [t . Iterable [ _R ]], t . AsyncGenerator [ _R ] ]],
5961 / ,
6062 * ,
63+ branch : True ,
6164 join : bool = False ,
6265 workers : int = 1 ,
6366 throttle : int = 0 ,
6467 multiprocess : bool = False ,
6568 bind : _ArgsKwargs = None ) -> AsyncPipeline [_P , _R ]: ...
66-
69+
6770 @t .overload
6871 def __new__ (
6972 cls ,
70- func : t .Callable [_P , t .AsyncGenerator [_R ]],
73+ func : t .Callable [_P , t .Awaitable [_R ]],
7174 / ,
7275 * ,
76+ branch : bool = False ,
7377 join : bool = False ,
7478 workers : int = 1 ,
7579 throttle : int = 0 ,
@@ -79,9 +83,10 @@ def __new__(
7983 @t .overload
8084 def __new__ (
8185 cls ,
82- func : t .Callable [_P , t .Generator [_R ]],
86+ func : t .Callable [_P , t .Iterable [_R ]],
8387 / ,
8488 * ,
89+ branch : True ,
8590 join : bool = False ,
8691 workers : int = 1 ,
8792 throttle : int = 0 ,
@@ -94,6 +99,7 @@ def __new__(
9499 func : t .Callable [_P , _R ],
95100 / ,
96101 * ,
102+ branch : bool = False ,
97103 join : bool = False ,
98104 workers : int = 1 ,
99105 throttle : int = 0 ,
@@ -105,15 +111,16 @@ def __new__(
105111 func : t .Optional [t .Callable ] = None ,
106112 / ,
107113 * ,
114+ branch : bool = False ,
108115 join : bool = False ,
109116 workers : int = 1 ,
110117 throttle : int = 0 ,
111118 multiprocess : bool = False ,
112119 bind : _ArgsKwargs = None ):
113120 # Classic decorator trick: @task() means func is None, @task without parentheses means func is passed.
114121 if func is None :
115- return functools .partial (cls , join = join , workers = workers , throttle = throttle , multiprocess = multiprocess , bind = bind )
116- return Pipeline ([Task (func = func , join = join , workers = workers , throttle = throttle , multiprocess = multiprocess , bind = bind )])
122+ return functools .partial (cls , branch = branch , join = join , workers = workers , throttle = throttle , multiprocess = multiprocess , bind = bind )
123+ return Pipeline ([Task (func = func , branch = branch , join = join , workers = workers , throttle = throttle , multiprocess = multiprocess , bind = bind )])
117124
118125 @staticmethod
119126 def bind (* args , ** kwargs ) -> _ArgsKwargs :
@@ -131,4 +138,3 @@ def f(x: int, y: int):
131138 if not args and not kwargs :
132139 return None
133140 return args , kwargs
134-
0 commit comments