Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If there are multiple workers, The scheduler can also start multiple times, causing duplicate execution. How to improve and avoid #8

Open
kanbang opened this issue Dec 24, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@kanbang
Copy link

kanbang commented Dec 24, 2024

If there are multiple workers, The scheduler can also start multiple times, causing duplicate execution.
How to improve and avoid

@kanbang
Copy link
Author

kanbang commented Dec 24, 2024

uvicorn app.main:app --workers 3

@VannySothea
Copy link
Owner

If there are multiple workers, The scheduler can also start multiple times, causing duplicate execution. How to improve and avoid

Thank you for reporting this issue! The behavior you're experiencing happens because, in a multi-worker environment, each worker starts its own instance of the scheduler, leading to duplicate task execution.

This is a common challenge in applications using schedulers with multiple workers. Here are some ways to address it:

  1. Run the scheduler in a separate, dedicated process (e.g., using Celery or APScheduler as a standalone service).
  2. Use a distributed lock (e.g., Redis-based locking) to ensure only one worker executes the scheduled tasks.
  3. Use a task queue to decouple task scheduling and execution.

I will update the documentation to include these best practices.

@VannySothea VannySothea added bug Something isn't working documentation Improvements or additions to documentation labels Dec 25, 2024
@VannySothea VannySothea moved this from Backlog to Todo in fastapi-user-authentication Dec 25, 2024
@VannySothea VannySothea changed the title Great Work!If there are multiple workers, The scheduler can also start multiple times If there are multiple workers, The scheduler can also start multiple times, causing duplicate execution. How to improve and avoid Dec 25, 2024
@VannySothea VannySothea removed the bug Something isn't working label Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
Status: Todo
Development

No branches or pull requests

2 participants