如何同时执行两个任务?Celery定时任务失效问题求助
Troubleshooting Your Celery Beat Task Not Running
Hey there, let's figure out why your 10-second scheduled task isn't working while your long-running infinite loop task runs fine with celeryd. I've dealt with similar beat issues before, so here are the key checks to run through:
1. Verify Celery Beat Can Find Your Task Configuration
- First, make sure you're starting beat with the correct app context. When running the beat command, you must point it to your Celery instance using the
-Aflag, like:
Without this, beat has no idea where your task definitions or schedule config lives.celery -A your_project_name.app beat -l info - Double-check your schedule setup. Your task should be referenced correctly in the
beat_scheduleconfig, using its full module path. For example:
Don't forget to decorate your write function withfrom celery import Celery from datetime import timedelta app = Celery('your_project') # Define your scheduled task app.conf.beat_schedule = { 'write-date-every-10-seconds': { 'task': 'your_app_module.write_date_to_txt', # Full path to your task function 'schedule': timedelta(seconds=10), }, }@app.taskso Celery recognizes it as a task.
2. Confirm Celery Beat Is Actually Running (and Logging)
- It's easy to assume beat is running, but it might crash silently due to config issues. Run beat in the foreground with verbose logging (
-l info) and watch for lines like:Scheduler: Sending task your_app_module.write_date_to_txt [uuid]
If you don't see this, beat either isn't detecting your task or failed to start. - If you're running beat as a daemon (like celeryd), check the beat log file for errors—common issues include missing task modules, broker connection failures, or permission errors when writing to your TXT file.
3. Ensure Worker and Beat Share the Same Configuration
- Both your worker (celeryd) and beat must use the same Celery app instance and broker settings. If you start the worker with
celery -A your_project.app worker, beat must use the same-A your_project.appflag. Mismatched broker URLs or task namespaces can prevent beat's tasks from being picked up by the worker. - Also, if your worker only has one concurrency process, your infinite loop task might be hogging it completely. Try increasing worker concurrency to leave room for scheduled tasks:
celery -A your_project.app worker -l info --concurrency=3
4. Test the Scheduled Task Function Directly
- Your write-to-TXT function might have silent failures that are stopping it from executing. Try running it manually in a Python shell to check:
Look for issues like incorrect file paths, missing write permissions, or uncaught exceptions in the function.from your_app_module import write_date_to_txt write_date_to_txt()
5. Reset the Beat Scheduler State
- Celery Beat uses a persistent scheduler file (
celerybeat-scheduleby default) to track the last run time of tasks. If this file is corrupted or has incorrect permissions, it can break scheduling. Delete this file, then restart beat to let it generate a fresh one.
Start with checking the beat logs—they're usually the fastest way to pinpoint the problem. If you hit specific error messages, we can dive deeper into those!
内容的提问来源于stack exchange,提问作者MohamadHossein
相关产品推荐
相关产品推荐

