在Django缓存API中设置一个对象由于pickle错误而失败

时间:2022-10-14 00:24:31

I'm trying to manually set an object in the Django cache API but it fails (i think due to pickling?) The object is given to me by a third party, my code is:

我尝试在Django缓存API中手动设置一个对象,但它失败了(我认为是pickle ?)对象是由第三方提供给我的,我的代码是:

def index(request, template_name="mytemplate.htm"):
    user_list = cache.get("user_list_ds")
    if user_list is None:
            # this is the expensive bit I'm trying to cache
            # given to me by a third part
        user_list = graffiti.user_list("top", 100).responseObj().blocks()
        cache.set("user_list_ds", user_list, 10*60) # 10 minutes

    return render_to_response(template_name, { 'user_list' : user_list,}, context_instance = RequestContext(request))

When I run this I get an error;

当我运行这个时,我得到一个错误;

Can't pickle <type 'etree._Element'>: import of module etree failed
in -    cache.set("user_list_ds", user_list, 10*60) # 10 minutes 

I'm very new to python, and I'm wondering how best to resolve this, do i need to pickle something first?

我对python非常陌生,我想知道如何最好地解决这个问题,我需要先提取一些东西吗?

1 个解决方案

#1


2  

It appears that you need to install ElementTree, because the pickle operation tries and fails to import the etree module.

看起来您需要安装ElementTree,因为pickle操作尝试并没有导入etree模块。

UPDATE: Looking at it further, are you actually trying to cache document nodes? If you're trying to cache the data from the node, you probably need to do some processing of the value you're currently storing in user_list.

更新:进一步观察,您是否正在尝试缓存文档节点?如果您试图从节点缓存数据,您可能需要对当前存储在user_list中的值进行一些处理。

#1


2  

It appears that you need to install ElementTree, because the pickle operation tries and fails to import the etree module.

看起来您需要安装ElementTree,因为pickle操作尝试并没有导入etree模块。

UPDATE: Looking at it further, are you actually trying to cache document nodes? If you're trying to cache the data from the node, you probably need to do some processing of the value you're currently storing in user_list.

更新:进一步观察,您是否正在尝试缓存文档节点?如果您试图从节点缓存数据,您可能需要对当前存储在user_list中的值进行一些处理。