如何使用 celery 任务创建单个可执行文件

hap*_*air 5 python django pyinstaller celery

我已经成功学习了 Celery 的一些基础知识,但我发现没有简单的方法来创建单个文件可执行文件(不需要将 celerybeat 作为单独的进程运行来运行定期任务)。可以编写一个应用程序并启动其工作进程(http://docs.celeryproject.org/en/3.1/userguide/application.html):

from datetime import timedelta
from celery import Celery
app = Celery()

@app.task
def test():
    print("he-he")

app.conf.update(
    BROKER_URL="redis://localhost:6379",
    CELERY_RESULT_BACKEND="redis://localhost:6379",
    CELERY_ACCEPT_CONTENT=["application/json"],
    CELERY_TASK_SERIALIZER="json",
    CELERY_RESULT_SERIALIZER="json",
    CELERYBEAT_SCHEDULE={
        'runs-every-30-seconds': {
            'task': '__main__.test',
            'schedule': timedelta(seconds=30),
        },
    }
)

if __name__ == '__main__':
   app.worker_main()
Run Code Online (Sandbox Code Playgroud)

但是我如何从同一模块启动beat进程以开始运行定期任务(为了不将celerybeat守护进程作为单独的可执行文件运行)?这很重要,因为我想使用 pyinstaller,所以客户端计算机上没有专用的 Python 解释器。

hap*_*air 3

人们可以像这样运行创建简单的节拍过程:

# that's the class to run the beat process
from celery.bin.beat import beat
# your app
from celery_tasks import app

if __name__ == "__main__":
    beat(app=app).run()
Run Code Online (Sandbox Code Playgroud)