【发布时间】:2014-03-23 00:11:45
【问题描述】:
我目前正在使用 Andy McCurdy 的 redis.py 模块与 Django 中的 Redis 进行交互
我使用 Celery 将某些任务卸载到后台。
这是我的任务之一:
import redis
pool = redis.ConnectionPool(host='XX.XXX.XXX.X', port=6379, db=0, password='password')
r_server = redis.Redis(connection_pool=pool)
pipe = r_server.pipeline()
# The number of seconds for two months
seconds = 5356800
@shared_task
def Activity(userID, object_id, timestamp):
timestamp = int(timestamp)
# Create Individual activity for each.
activity_key = 'invite:activity:%s:%s' % (userID, timestamp)
mapping = dict(
user_id = userID,
object_id = object_id)
pipe.hmset(activity_key, mapping).expire(activity_key, seconds).execute()
每当调用此任务时,我都会收到以下错误:
AttributeError: 'bool' object has no attribute 'expire'
这可能是什么原因造成的?
我后来在 python 控制台中做了一个测试,看看我的语法是否有问题,但一切都按我的计划进行。那么什么可能导致这个错误呢?
更新
我认为 expire 正在评估 hmset(activity_key, mapping) 的结果。这很奇怪! expire 是一种管道方法。
第二次更新
我暂时找到了解决办法。似乎这只发生在 Celery 中。本机 Django 视图和 python 控制台不表现出这种行为。它似乎在评估它之前的表达式的结果。如果你们中的任何人遇到这个问题,这里有一个解决方法。
pipe.hmset(activity_key, mapping)
pipe.lpush('mylist', 1)
pipe.expire('mylist', 300)
pipe.execute()
这应该有效,不会给您带来任何问题。快乐编码!
【问题讨论】:
标签: python django redis celery