You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何同时执行两个任务?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 -A flag, like:
    celery -A your_project_name.app beat -l info
    
    Without this, beat has no idea where your task definitions or schedule config lives.
  • Double-check your schedule setup. Your task should be referenced correctly in the beat_schedule config, using its full module path. For example:
    from 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),
        },
    }
    
    Don't forget to decorate your write function with @app.task so 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.app flag. 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:
    from your_app_module import write_date_to_txt
    write_date_to_txt()
    
    Look for issues like incorrect file paths, missing write permissions, or uncaught exceptions in the function.

5. Reset the Beat Scheduler State

  • Celery Beat uses a persistent scheduler file (celerybeat-schedule by 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:14:51