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
21import logging
32from functools import wraps
43from gevent import getcurrent
54
65from .thread import TaskThread
76
8- from flask import copy_current_request_context
7+ from flask import copy_current_request_context , has_request_context
98
109
1110class TaskMaster :
@@ -38,21 +37,26 @@ def states(self):
3837
3938 def new (self , f , * args , ** kwargs ):
4039 # 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 )
4445 self ._tasks .append (task )
4546 return task
4647
4748 def remove (self , task_id ):
4849 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 )
5152
5253 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 ]
5660
5761
5862# Task management
You can’t perform that action at this time.
0 commit comments