File tree 1 file changed +12
-0
lines changed
1 file changed +12
-0
lines changed Original file line number Diff line number Diff line change 7
7
import sys
8
8
import traceback
9
9
import tempfile
10
+ import threading
10
11
from asgiref .sync import sync_to_async
11
12
from datetime import timedelta
12
13
from gettext import gettext as _
@@ -125,6 +126,12 @@ def _execute_task(task):
125
126
send_task_notification (task )
126
127
127
128
129
+ def running_from_thread_pool () -> bool :
130
+ thread_name = threading .current_thread ().name
131
+ # ThreadPoolExecutor names threads like: "ThreadPoolExecutor-0_0"
132
+ return "ThreadPoolExecutor" in thread_name
133
+
134
+
128
135
def dispatch (
129
136
func ,
130
137
args = None ,
@@ -174,6 +181,11 @@ def dispatch(
174
181
175
182
assert deferred or immediate , "A task must be at least `deferred` or `immediate`."
176
183
184
+ # Can't run short tasks immediately if running from thread pool
185
+ force_defer = True if running_from_thread_pool () else False
186
+ short = immediate
187
+ immediate = short and not force_defer
188
+
177
189
if callable (func ):
178
190
function_name = f"{ func .__module__ } .{ func .__name__ } "
179
191
else :
You can’t perform that action at this time.
0 commit comments