Skip to content

Commit c1a509d

Browse files
authored
Merge pull request #64 from labthings/fix-task-request-context
Fix task request context
2 parents 1834104 + dbaff52 commit c1a509d

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

labthings/core/tasks/thread.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ def __init__(self, target, *args, **kwargs):
3636
# Nice string representation of target function
3737
self.target_string = f"{self._target}(args={self._args}, kwargs={self._kwargs})"
3838

39+
# copy_current_request_context allows threads to access flask current_app
40+
if has_request_context():
41+
logging.debug(f"Copying request context to {self._target}")
42+
self._target = copy_current_request_context(self._target)
43+
else:
44+
logging.debug("No request context to copy")
45+
3946
# Private state properties
4047
self._status: str = "idle" # Task status
4148
self._return_value = None # Return value
@@ -79,12 +86,7 @@ def update_data(self, data: dict):
7986
self.data.update(data)
8087

8188
def _run(self): # pylint: disable=E0202
82-
# copy_current_request_context allows threads to access flask current_app
83-
if has_request_context():
84-
target = copy_current_request_context(self._target)
85-
else:
86-
target = self._target
87-
return self._thread_proc(target)(*self._args, **self._kwargs)
89+
return self._thread_proc(self._target)(*self._args, **self._kwargs)
8890

8991
def _thread_proc(self, f):
9092
"""

0 commit comments

Comments
 (0)