0

我将 Redis 和 Celery 一起用于 django 项目。

[前提]

django==1.5.4
Redis==2.2.4
Celery==3.0.23
django-redis==3.7.1
django-celery==3.0.23

【目录结构】

Project/
     apps/
         app_1/
             views.py
                 def get_something():
         utils/
             redis.py
                 def do_stuff(): // do something related with Redis
             tasks.py
                 @task()
                 def do_stuff(): // execute do_stuff() at redis.py

[问题]

views.py中,

from utils.redis import do_stuff
from utils.tasks import do_stuff


def get_something():

    do_stuff.delay()  => Execute task by celery (Normal)

    do_stuff()        => Executed do_stuff from tasks.py, not from redis.py
                         Making a recursion error (Unusual)
                         Expected to execute do_stuff from redis.py

函数名称重叠时,如何处理Celery仅通过“延迟方法”执行。

提前致谢。

4

1 回答 1

0

出于显而易见的原因,在任何 Python 代码中都不能有两个相同的名称指向不同的事物。您可以通过导入模块而不是函数来更轻松地区分它们:

from utils import redis
from utils import tasks


def get_something():
    redis.do_stuff.delay()
    tasks.do_stuff()

但还要注意,您可以不使用 直接调用 Celery 任务delay(),这会导致它们立即在进程中执行,而不是通过 Celery。

于 2014-09-08T09:09:45.823 回答