準(zhǔn)備
在master和client機(jī)器上都安裝celery彪杉,redis和相關(guān)軟件
軟件版本要一致炎辨,否則在task的傳遞過程中可能會出問題
master機(jī)器調(diào)用client機(jī)器上的worker來進(jìn)行計算
master的ip是1.1.1.1 client的ip是2.2.2.2
master上的配置
在master上啟動redis server作為celery的backend玲献,redis的ip綁定為1.1.1.1
準(zhǔn)備文件:
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上配置
準(zhǔn)備文件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上運(yùn)行準(zhǔn)備的文件
看到結(jié)果
True
3