简体   繁体   English

Django:AttributeError:'bool'对象没有属性'expire'

[英]Django: AttributeError: 'bool' object has no attribute 'expire'

I'm currently using Andy McCurdy's redis.py module to interact with Redis from Django 我目前正在使用Andy McCurdy的redis.py模块与Django的Redis进行交互

I offload certain tasks into the background using Celery. 我使用Celery将某些任务卸载到后台。

Here is one of my tasks: 这是我的任务之一:

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()

Whenever this task is invoked, I get the following error: 每当调用此任务时,我都会收到以下错误:

AttributeError: 'bool' object has no attribute 'expire'

What could possibly be causing this? 什么可能导致这个?

I later did a test in a python console to see if there was something wrong with my syntax, but everything worked just as I had planned. 我后来在python控制台上做了一个测试,看看我的语法是否有问题,但是一切都按照我的计划进行。 So what could possibly be causing this error? 那么什么可能导致这个错误?

UPDATE UPDATE

I think the expire is evaluating the result of hmset(activity_key, mapping). 我认为过期是评估hmset(activity_key,mapping)的结果。 This is weird! 这很奇怪! expire is a method for pipe. expire是一种管道方法。

SECOND UPDATE 第二次更新

I found a work around for the time being. 我暂时找到了解决方法。 It seems this only occurs within Celery. 看来这只发生在芹菜中。 Native Django views and the python console don't exhibit this behavior. 本机Django视图和python控制台不会出现此行为。 It seems to evaluate the result of the expression before it. 它似乎在评估它之前的表达式的结果。 If any of you come across this problem, here is a work around. 如果你们中的任何一个遇到这个问题,这是一个解决方法。

pipe.hmset(activity_key, mapping)
pipe.lpush('mylist', 1)
pipe.expire('mylist', 300)
pipe.execute()

This should work and not give you any issues. 这应该工作,而不是给你任何问题。 Happy Coding! 快乐的编码!

pipe.hmset() does not return the pipeline; pipe.hmset() 返回管道; you cannot chain the calls here. 你不能在这里链接电话。 Instead, a boolean is returned (probably indicating if the hmset() call succeeded). 相反,返回一个布尔值(可能表示hmset()调用是否成功)。

Call the .expire() and .execute() methods separately: 分别调用.expire().execute()方法:

pipe.hmset(activity_key, mapping)
pipe.expire(activity_key, seconds)
pipe.execute()

I suspect you need to create a pipeline within the Celery task instead of re-using a global here. 我怀疑你需要在Celery任务中创建一个管道,而不是在这里重新使用全局。 Move the pipe = r_server.pipeline() to within the activity. pipe = r_server.pipeline()移动到活动中。

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

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