从脚本中启动Celery的worker

使用Celery与Flask的博客文章《使用Celery和Flask的示例代码》中包含了该存储库。

准备Redis服务器

#!/bin/bash
if [ ! -d redis-stable/src ]; then
    curl -O http://download.redis.io/redis-stable.tar.gz
    tar xvzf redis-stable.tar.gz
    rm redis-stable.tar.gz
fi
cd redis-stable
make
src/redis-server

提供脚本

worker.py
proj/
  celery.py
  tasks.py
  __init__.py
from proj import app

# celery -A proj worker -l INFO
app.worker_main(['worker', '--loglevel', 'info'])
from celery import Celery

app = Celery('proj',
             broker='redis://localhost:6379/0',
             backend='redis://localhost:6379/0',
             include=['proj.tasks'])

# Optional configuration, see the application user guide.
app.conf.update(
    result_expires=3600,
)

if __name__ == '__main__':
    app.start()
from .celery import app

@app.task
def add(x, y):
    return x + y

@app.task
def mul(x, y):
    return x * y

@app.task
def xsum(numbers):
    return sum(numbers)
from .celery import app

执行 (shí

$ python worker.py
广告
将在 10 秒后关闭
bannerAds