32
32
MIN_PRIORITY = - 100
33
33
MAX_PRIORITY = 100
34
34
DEFAULT_PRIORITY = 0
35
+ DEFAULT_TIMEOUT = 600 # 10 minutes
35
36
36
37
TASK_REFRESH_ATTRS = {
37
38
"_exception_class" ,
@@ -78,6 +79,9 @@ class Task(Generic[P, T]):
78
79
immediately, or whatever the backend decides
79
80
"""
80
81
82
+ timeout : int = DEFAULT_TIMEOUT
83
+ """The maximum duration the task can take to execute before being aborted"""
84
+
81
85
def __post_init__ (self ) -> None :
82
86
self .get_backend ().validate_task (self )
83
87
@@ -95,6 +99,7 @@ def using(
95
99
queue_name : Optional [str ] = None ,
96
100
run_after : Optional [Union [datetime , timedelta ]] = None ,
97
101
backend : Optional [str ] = None ,
102
+ timeout : Optional [int ] = None ,
98
103
) -> Self :
99
104
"""
100
105
Create a new task with modified defaults
@@ -110,6 +115,8 @@ def using(
110
115
changes ["run_after" ] = run_after
111
116
if backend is not None :
112
117
changes ["backend" ] = backend
118
+ if timeout is not None :
119
+ changes ["timeout" ] = timeout
113
120
114
121
return replace (self , ** changes )
115
122
@@ -188,6 +195,7 @@ def task(
188
195
queue_name : str = DEFAULT_QUEUE_NAME ,
189
196
backend : str = DEFAULT_TASK_BACKEND_ALIAS ,
190
197
enqueue_on_commit : Optional [bool ] = None ,
198
+ timeout : int = DEFAULT_TIMEOUT ,
191
199
) -> Callable [[Callable [P , T ]], Task [P , T ]]: ...
192
200
193
201
@@ -199,6 +207,7 @@ def task(
199
207
queue_name : str = DEFAULT_QUEUE_NAME ,
200
208
backend : str = DEFAULT_TASK_BACKEND_ALIAS ,
201
209
enqueue_on_commit : Optional [bool ] = None ,
210
+ timeout : int = DEFAULT_TIMEOUT ,
202
211
) -> Union [Task [P , T ], Callable [[Callable [P , T ]], Task [P , T ]]]:
203
212
"""
204
213
A decorator used to create a task.
@@ -212,6 +221,7 @@ def wrapper(f: Callable[P, T]) -> Task[P, T]:
212
221
queue_name = queue_name ,
213
222
backend = backend ,
214
223
enqueue_on_commit = enqueue_on_commit ,
224
+ timeout = timeout ,
215
225
)
216
226
217
227
if function :
@@ -223,7 +233,7 @@ def wrapper(f: Callable[P, T]) -> Task[P, T]:
223
233
@dataclass (frozen = True )
224
234
class TaskResult (Generic [T ]):
225
235
task : Task
226
- """The task for which this is a result"""
236
+ """The task for which this is a result, as it was run """
227
237
228
238
id : str
229
239
"""A unique identifier for the task result"""
0 commit comments