简体   繁体   中英

Run Celery beat and worker concurrently in FLASK app

I am working on a FLASK app with Celery. In it, I run the app using the line app.run() . If I change it to the following

if __name__ == '__main__':

application = current_app._get_current_object()

worker = worker.worker(app=application)
beat = beat.beat(app=application)

options = {
    'broker': app.config['CELERY_BROKER_URL'],
    'loglevel': 'INFO',
    'traceback': True,
}

worker.run(**options)
beat.run(**options)
app.run()

This runs the app and the worker, but the Celery beat does not run until I shut down the worker. Is there any way that I can run both concurrently? Maybe add the -B flag to the options settings somehow? I'd appreciate any help on the matter.

If you add 'beat' = True to your options, when you call worker.run(**options) it'll give the beat task to a worker:

if __name__ == '__main__':

application = current_app._get_current_object()

worker = worker.worker(app=application)

options = {
    'broker': app.config['CELERY_BROKER_URL'],
    'loglevel': 'INFO',
    'traceback': True,
    'beat': True,
}

worker.run(**options)
app.run()

It should stop the beat when the worker stops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM