LoginSignup
0
0

More than 1 year has passed since last update.

Celeryのwokerをスクリプトから起動

Last updated at Posted at 2021-09-20

python - スクリプト/モジュール__main__からCeleryワーカーを起動する方法は?
miguelgrinberg/flask-celery-example: This repository contains the example code for my blog article Using Celery with Flask.

redisサーバーの準備

github.com/miguelgrinberg/flask-celery-example/blob/master/run-redis.sh
#!/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
worker.py
from proj import app

# celery -A proj worker -l INFO
app.worker_main(['worker', '--loglevel', 'info'])
proj/celery.py
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()
proj/tasks.py
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)
__ini__.py
from .celery import app

実行

$ python worker.py
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0