在 Linux 上安装 Celery,您需要遵循以下步骤:
python --version
pip --version
如果没有安装,请使用以下命令安装 Python 和 pip:
# 对于 Ubuntu/Debian 系统
sudo apt-get update
sudo apt-get install python python-pip
# 对于 CentOS/RHEL 系统
sudo yum install python python-pip
pip install celery
# 对于 Ubuntu/Debian 系统
echo 'deb http://www.rabbitmq.com/debian/ testing main' | sudo tee /etc/apt/sources.list.d/rabbitmq.list > /dev/null
wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install rabbitmq-server
sudo systemctl enable rabbitmq-server
sudo systemctl start rabbitmq-server
# 对于 CentOS/RHEL 系统
sudo yum install epel-release
sudo yum install rabbitmq-server
sudo systemctl enable rabbitmq-server
sudo systemctl start rabbitmq-server
from celery import Celery
app = Celery('tasks', broker='pyamqp://guest@localhost//')
@app.task
def add(x, y):
return x + y
celery -A tasks worker --loglevel=info
add.delay()
方法将任务添加到队列中:result = add.delay(4, 4)
print(result.get()) # 输出:8
现在,您已经在 Linux 上安装了 Celery,并成功运行了一个简单的任务。您可以根据需要创建更多复杂的任务并运行它们。