Skip to content

Conversation

@jainal09
Copy link

Summary

  • Modify BackgroundTasks.__call__ to remove tasks from the list after execution
  • Add test to verify tasks are removed after processing

This enables users to monitor the queue length for implementing features like graceful shutdowns. Previously, all tasks remained in the list even after execution, making it impossible to determine how many tasks were still pending.

Use Case

@app.get("/get_queue_length/")
async def read_task():
    return {"task_length": len(tasks.tasks)}

This is useful for:

  • Graceful shutdown handling (wait for all background tasks to complete before stopping)
  • Queue monitoring and observability

Related

Test Plan

  • Added test_tasks_removed_after_execution to verify tasks list is empty after execution
  • All existing background task tests pass

This change modifies BackgroundTasks.__call__ to remove tasks from the
list after they are executed. This enables users to monitor the queue
length for implementing features like graceful shutdowns.

Previously, all tasks remained in the list even after execution, making
it impossible to determine how many tasks were still pending.

Closes Kludex#2542
@jainal09 jainal09 closed this Dec 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant