远程调用celery

2017-11-26  本文已影响0人  ldh123

准备

在master和client机器上都安装celery,redis和相关软件
软件版本要一致,否则在task的传递过程中可能会出问题
master机器调用client机器上的worker来进行计算
master的ip是1.1.1.1 client的ip是2.2.2.2

master上的配置

在master上启动redis server作为celery的backend,redis的ip绑定为1.1.1.1
准备文件:

from celery import Celery
import time

broker = 'redis://1.1.1.1:6379'
backend = 'redis://1.1.1.1:6379'


app = Celery('tasks', broker=broker, backend=backend, task_serializer='pickle')
app.conf.update(CELERY_ACCEPT_CONTENT = ['pickle'], CELERY_RESULT_SERIALIZER = 'pickle')

@app.task(serializer="pickle")
def add(x, y):
    return x + y


result = app.send_task('task.add', [1,2])
time.sleep(1)
print(result.ready())
print(result.result)

在client上配置

准备文件task.py:

from celery import Celery
import time

broker = 'redis://1.1.1.1:6379'
backend = 'redis://1.1.1.1:6379'


app = Celery('tasks', broker=broker, backend=backend, task_serializer='pickle')
app.conf.update(CELERY_ACCEPT_CONTENT = ['pickle'], CELERY_RESULT_SERIALIZER = 'pickle')

@app.task(serializer="pickle")
def add(x, y):
    return x + y

启动celery的worker

celery -A task worker  --loglevel=info

在master上运行准备的文件

看到结果

True
3
上一篇下一篇

猜你喜欢

热点阅读