0. 环境

  • 服务器: ubuntu 18
  • 开发语言: python 3

1. 配置服务环境

安装docker

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

设置docker镜像源,加速下载

mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://bjtzu1jb.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

下载redis、mysql

docker pull redis
docker pull mysql

安装docker-compose

pip3 install docker-compose -i https://pypi.tuna.tsinghua.edu.cn/simple
# 或:
# curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose

配置docker-compose.yml

version: '2'

services:
  redis:
    image: 'docker.io/bitnami/redis:6.0-debian-10'
    environment:
      - ALLOW_EMPTY_PASSWORD=no
      - REDIS_PASSWORD=bblnXxa1
      - REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL
    ports:
      - '6379:6379'
    volumes:
      - 'redis_data:/bitnami/redis/data'
  mysql:
    image: mysql
    ports:
      - "3389:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=bblnXxa1
    volumes:
      - ./mysql:/var/lib/mysql
    networks:
      - basic

volumes:
  redis_data:
    driver: local

networks:
  basic:

2. 配置开发环境

创建纯净的虚拟环境

virtualenv --no-site-packages venv

安装python库

pip install "celery[redis]" -i https://pypi.tuna.tsinghua.edu.cn/simple

3. 编写测试代码

tasks.py

from celery import Celery

BROKER_URL = 'redis://:[email protected]:6379/0'
BACKEND_URL = 'redis://:[email protected]:6379/1'
app = Celery('tasks', broker=BROKER_URL, backend=BACKEND_URL)

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

运行

celery -A tasks worker --loglevel=info
如果觉得我的文章对你有用,请随意赞赏