How to start a Celery worker from a script/module __main__?

前端 未结 5 853
离开以前
离开以前 2020-12-13 07:07

I\'ve define a Celery app in a module, and now I want to start the worker from the same module in its __main__, i.e. by running the module with

相关标签:
5条回答
  • 2020-12-13 07:46

    I think you are just missing wrapping the args so celery can read them, like:

    queue = Celery('blah', include=['blah'])
    queue.start(argv=['celery', 'worker', '-l', 'info'])
    
    0 讨论(0)
  • 2020-12-13 07:48

    worker_main was put back in celery 5.0.3 here: https://github.com/celery/celery/pull/6481

    This worked for me on 5.0.4:

    self.app.worker_main(argv = ['worker', '--loglevel=info', '--concurrency={}'.format(os.environ['CELERY_CONCURRENCY']), '--without-gossip'])
    
    0 讨论(0)
  • 2020-12-13 07:50

    using app.worker_main method (v3.1.12):

    ± cat start_celery.py
    #!/usr/bin/python
    
    from myapp import app
    
    
    if __name__ == "__main__":
        argv = [
            'worker',
            '--loglevel=DEBUG',
        ]
        app.worker_main(argv)
    
    0 讨论(0)
  • 2020-12-13 08:00

    Since Celery 5 things have been changed

    The worker_main results now:

    AttributeError: 'Celery' object has no attribute 'worker_main'
    

    For Celery 5 do following:

    app = celery.Celery(
        'project',
        include=['project.tasks']
    )
    
    if __name__ == '__main__':
        worker = app.Worker(
            include=['project.tasks']
        )
        worker.start()
    

    See here celery.apps.worker and celery.worker.WorkController.setup_defaults for details (hope it will be documented better in the future).

    0 讨论(0)
  • 2020-12-13 08:03

    Based on code from Django-Celery module you could try something like this:

    from __future__ import absolute_import, unicode_literals
    
    from celery import current_app
    from celery.bin import worker
    
    
    if __name__ == '__main__':
        app = current_app._get_current_object()
    
        worker = worker.worker(app=app)
    
        options = {
            'broker': 'amqp://guest:guest@localhost:5672//',
            'loglevel': 'INFO',
            'traceback': True,
        }
    
        worker.run(**options)
    
    0 讨论(0)
提交回复
热议问题