File tree Expand file tree Collapse file tree 1 file changed +14
-10
lines changed Expand file tree Collapse file tree 1 file changed +14
-10
lines changed Original file line number Diff line number Diff line change 1
- import threading
2
1
import logging
3
2
from functools import wraps
4
3
from gevent import getcurrent
5
4
6
5
from .thread import TaskThread
7
6
8
- from flask import copy_current_request_context
7
+ from flask import copy_current_request_context , has_request_context
9
8
10
9
11
10
class TaskMaster :
@@ -38,21 +37,26 @@ def states(self):
38
37
39
38
def new (self , f , * args , ** kwargs ):
40
39
# copy_current_request_context allows threads to access flask current_app
41
- task = TaskThread (
42
- target = copy_current_request_context (f ), args = args , kwargs = kwargs
43
- )
40
+ if has_request_context ():
41
+ target = copy_current_request_context (f )
42
+ else :
43
+ target = f
44
+ task = TaskThread (target = target , args = args , kwargs = kwargs )
44
45
self ._tasks .append (task )
45
46
return task
46
47
47
48
def remove (self , task_id ):
48
49
for task in self ._tasks :
49
- if (task .id == task_id ) and not task .isAlive () :
50
- del task
50
+ if (str ( task .id ) == str ( task_id )) and task .dead :
51
+ self . _tasks . remove ( task )
51
52
52
53
def cleanup (self ):
53
- for task in self ._tasks :
54
- if not task .isAlive ():
55
- del task
54
+ for i , task in enumerate (self ._tasks ):
55
+ if task .dead :
56
+ # Mark for delection
57
+ self ._tasks [i ] = None
58
+ # Remove items marked for deletion
59
+ self ._tasks = [t for t in self ._tasks if t ]
56
60
57
61
58
62
# Task management
You can’t perform that action at this time.
0 commit comments