Skip to content

Commit 3f7d79a

Browse files
committed
Fix excessive CPU usage when no tasks in queue
1 parent 2f8b0ad commit 3f7d79a

File tree

1 file changed

+15
-12
lines changed
  • django_tasks/backends/database/management/commands

1 file changed

+15
-12
lines changed

django_tasks/backends/database/management/commands/db_worker.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,27 @@ def start(self) -> None:
7070
with transaction.atomic():
7171
task_result = tasks.get_locked()
7272

73-
if task_result is None:
74-
if self.batch:
75-
# If we're running in "batch" mode, terminate the loop (and thus the worker)
76-
return None
77-
else:
78-
# Otherwise, check for more jobs
79-
continue
80-
81-
# "claim" the task, so it isn't run by another worker process
82-
task_result.claim()
83-
84-
self.run_task(task_result)
73+
if task_result is not None:
74+
# "claim" the task, so it isn't run by another worker process
75+
task_result.claim()
76+
77+
if task_result is not None:
78+
self.run_task(task_result)
79+
8580
finally:
8681
self.running_task = False
8782

83+
if self.batch and task_result is None:
84+
# If we're running in "batch" mode, terminate the loop (and thus the worker)
85+
return None
86+
87+
# Wait before checking for another task
8888
time.sleep(self.interval)
8989

9090
def run_task(self, db_task_result: DBTaskResult) -> None:
91+
"""
92+
Run the given task, marking it as complete or failed.
93+
"""
9194
try:
9295
task = db_task_result.task
9396
task_result = db_task_result.task_result

0 commit comments

Comments
 (0)