您可以使用app.worker_main() 将工作线程作为线程运行。
由于您能够从命令行运行它,我假设您已经为Celery 类构建了一个对象(在文档中通常称为app)。您可以尝试以下代码将worker作为线程运行。
from celeryconfig import app # config file you must have build earlier where app = Celery(), object of Celery class
def worker():
# Arguments you give on command line
argv = [
'worker','-A','<module>.tasks',
'-P','gevent', # Would probably need this argument if running with other Greenlets
'--loglevel=info']
app.worker_main(argv)
只需在模块的 main.py 文件中为上述方法创建一个线程即可。
对于 celery beat,我之前曾尝试自己构建类似的方法。 (不确定是否已经存在方法)。我写了下面的方法,添加到Celery类中
def beat_main(self, argv=None):
return instantiate(
'celery.bin.beat:beat',
app=self).execute_from_commandline(argv)
您的 Celery 课程是用 /usr/local/lib/python2.7/dist-packages/celery/app/base.py 编写的。尝试以与 worker 相同的方式使用它(app.beat_main 带参数)。希望它也适用于你。