5

I have a implemented a python command which starts celery

@click.command("tasks", help="run this command to start celery task queue")
def tasks():
    """
    Runs the celery task queue
    """
    from celery.bin import worker

    try:

        worker = worker.worker(app=app.config.get("task_app"))
        worker.run(app="my_app.task_queue", loglevel="info", uid=os.environ["uid"])
    except Exception as e:
        raise e

I need to create a similar command which starts celery beat and I had the following approach

@click.command("beat", help="run this command to start beat")
def beat():

    try:


        p = app.config.get("task_app") # gets the current 
        command = CeleryCommand()
        lst = ["celery", "beat"]
        command.execute_from_commandline(lst)

    except Exception as e:
        raise e

the --app parameter will not work with this approach. Is there a way to make this command programmatically work celery -A proj beat without passing from command line?

not 0x12
  • 19,360
  • 22
  • 67
  • 133

1 Answers1

4

To start celery beat programmatically:

app.Beat(loglevel='debug').run()

Alternatively:

from celery.apps.beat import Beat

b = Beat(app=your_celery_app, loglevel='debug')
b.run()

For keyword arguments, see celery.beat documentation.

Eric L
  • 592
  • 1
  • 6
  • 20