简体   繁体   English

在Celery Task中使用Redis连接和保存数据

[英]Connecting and Saving Data With Redis Inside Celery Task

I have an object that saves data to Redis. 我有一个将数据保存到Redis的对象。 It needs to block as less as possible, so I've decided to use Celery to offload the task. 它需要尽可能少地阻塞,因此我决定使用Celery减轻任务负担。 When I try to .save() the object outside of celery, it connects to Redis and stores the data just fine. 当我尝试将对象保存在celery外部时,它连接到Redis并存储数据。 However, when I try to do the exact same thing from a Celery task, it looks like it runs, but there is no connection to Redis, no exception, no error output and nothing gets saves to the Redis server. 但是,当我尝试从Celery任务执行完全相同的操作时,它看起来像在运行,但是没有与Redis的连接,没有异常,没有错误输出,也没有任何内容保存到Redis服务器。 I replicated the problem with the small bit of code below. 我用下面的少量代码复制了这个问题。 test.py: test.py:

from celery.decorators import task
import redis

class A(object):
    def __init__(self):
        print "init"

    def save(self):
        self.r = self.connect()
        self.r.set('foo', 'bar')
        print "saved"

    def connect(self):
        return redis.Redis(host="localhost", port=6379)

a = A()

@task
def something(a):
    a.save()

Here is the Python console output: 这是Python控制台输出:

>>> from test import *
init
>>> a
<test.A object at 0x1010e3c10>
>>> result = something.delay(a)
>>> result.ready()
True
>>> result.successful()
True

And here is the celeryd output: 这是芹菜的输出:

[2010-11-15 12:05:33,672: INFO/MainProcess] Got task from broker: test.something[d1d71ee5-7206-4fa7-844c-04445fd8bead]
[2010-11-15 12:05:33,688: WARNING/PoolWorker-2] saved
[2010-11-15 12:05:33,694: INFO/MainProcess] Task test.something[d1d71ee5-7206-4fa7-844c-04445fd8bead] succeeded in 0.00637984275818s: None

Any help would be awesome! 任何帮助都是极好的! I've replicated the issue on multiple computers, with multiple python versions. 我已在具有多个python版本的多台计算机上复制了该问题。

The problem was being caused by a misconfiguration in the celeryconfig.py. 该问题是由celeryconfig.py中的错误配置引起的。 CELERY_IMPORTS needed to include the task module. CELERY_IMPORTS需要包含任务模块。 This is resolved. 解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM