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

发布于 2024-08-08 07:02:25 字数 816 浏览 12 评论 0原文

我试图在 Django 缓存 API 中手动设置一个对象,但失败了(我认为是由于酸洗?) 该对象是由第三方给我的,我的代码是:

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

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

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

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

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木槿暧夏七纪年 2024-08-15 07:02:25

看来您需要安装 ElementTree,因为pickle 操作尝试导入 etree 模块,但失败。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文