hello.py
# coding=utf-8
import time
from manage import app
from celery.contrib.methods import task_method
class HelloService(object):
@app.task(filter=task_method)
def say(self):
time.sleep(4)
print('hello!')
world.py
# coding=utf-8
import time
from celery.contrib.methods import task_method
from manage import app
class WorldService(object):
@app.task(filter=task_method)
def say(self):
time.sleep(4)
print('hello')
run.py
# coding=utf-8
from hello import HelloService
from world import WorldService
if __name__ == '__main__':
HelloService().say.delay()
WorldService().say.delay()
print('process done')
使用
celery worker -A hello --loglevel=INFO -n hello
celery worker -A world --loglevel=INFO -n world
分别启动两个 woker
执行 run.py
结果在 hello 的 worker 里报错 DecodeError: No module named world
而再 world 的 worker 里报错 DecodeError: No module named hello
尝试在 run.py 中只保留一个 service 的调用,注释另一个的 import.则不会出现此错误