python - 无法 pickle <type 'function' > : attribute lookup __builtin__. 函数失败

标签 python django pickle

我在我的 Django 应用程序中收到此错误,但是,它每天只发生一次或更少,而且事实证明它极难调试。

Environment:

Request Method: POST

Django Version: 1.3.1
Python Version: 2.6.6
Installed Applications:
['django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'fimedlabs',
 'data',
 'djcelery']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'fimedlabs.auth.userMiddleWare')


Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  178.                 response = middleware_method(request, response)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/sessions/middleware.py" in process_response
  36.                 request.session.save()
File "/usr/local/lib/python2.6/dist-packages/django/contrib/sessions/backends/db.py" in save
  57.             session_data = self.encode(self._get_session(no_load=must_create)),
File "/usr/local/lib/python2.6/dist-packages/django/contrib/sessions/backends/base.py" in encode
  93.         pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)

Exception Type: PicklingError at /
Exception Value: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

我试过这个问题的答案:

How to tell for which object attribute pickle fails?

通过将 self 添加到实际错误中以查看是否会打印 Django 错误中的任何内容而无济于事。

我在哪里可以打印出导致此错误出现问题的对象,以便它出现在 Django 错误页面中?

谢谢! ~马特

编辑:我存储在缓存中的唯一对象是一个带有代码的用户对象:

class user(object):
username = str()
userid = uuid.UUID(int=0)

client = models.Client()
clientid = uuid.UUID(int=0)
clientname = ''

data = models.User()
accesslevel = models.AccessLevel()

active = False
client_active = False
isFimed = False
isFimedAdmin = False
isClientAdmin = False
isFimedManager = False
mysettingsform = None
viewingas = False

menu = []

_exists = False
_authenticated = False

def __str__(self):
    return str(self.__dict__.copy())

def __getstate__(self):
    return self.__dict__.copy()

def __setstate__(self, dict):
    self.__dict__ = dict

def __init__(self, username=None):
    if username:
        self.initialize(username)

def initialize(self, username):
    self.username = username
    model = models.User.objects.filter(username=username).all()
    if len(model) == 1:
        model = model[0]
        self.data = model
        self._exists = True
        self.userid = self.data.id
        self.active = self.data.active
        self.isFimed = self.data.isFimed()
        self.isFimedAdmin = self.data.isFimedAdmin()
        self.isClientAdmin = self.data.isClientAdmin()
        self.isFimedManager = self.data.isFimedManager()
        self.mysettingsform = UserFormSelf(initial={"id":model.id, "username":model.username, "name":model.name, "email":model.email, "phone":model.phone})

        self.accesslevel = models.AccessLevel.objects.filter(id=self.data.accesslevel_id)[:1][0].level
        cli = self.data.client
        self.client = cli
        self.clientid = cli.id
        self.clientname = cli.name
        if cli.active:
            self.client_active = True

        model.lastlogin = datetime.datetime.now()
        model.save()

        self.menu = getMenu(self.data)
    else:
        self._exists = False

def authenticate(self, password):
    self._authenticated = False
    if (self.active == False or self.client_active == False):
        return False
    if self._exists:
        import hashlib
        hash = hashlib.md5('%s%s' % (str(password), self.data.pwsalt)).hexdigest()
        if hash == self.data.pwhash:
            self._authenticated = True
            return True
    return False

def updateUser(self):
    self.initialize(models.User.objects.filter(id=self.userid).get().username)

def mkContext(self):
    c = Context()
    c['menu'] = self.menu
    c['user'] = self
    c['language'] = language
    c['colors'] = colors
    c["isFimed"] = self.isFimed
    c["isFimedAdmin"] = self.isFimedAdmin
    c["isClientAdmin"] = self.isClientAdmin
    c["isFimedManager"] = self.isFimedManager
    c["mysettingsform"] = self.mysettingsform
    return c

编辑:Werkzeug 之后的 WSGI 文件:

import django.core.handlers.wsgi
djangoapplication = django.core.handlers.wsgi.WSGIHandler()
def application(environ, start_response):
    if 'SCRIPT_NAME' in environ:
        del environ['SCRIPT_NAME']
    return djangoapplication(environ, start_response)
# The following lines enable the werkzeug debugger
import django.views.debug
def null_technical_500_response(request, exc_type, exc_value, tb):
    raise exc_type, exc_value, tb
django.views.debug.technical_500_response = null_technical_500_response
from werkzeug.debug import DebuggedApplication
application = DebuggedApplication(application, evalex=True)

最佳答案

在我的例子中(与 Django 无关)当 lambda 作为目标函数传递时,此异常由 multiprocessing.Pool.map 抛出。创建一个命名函数并通过 initargs 参数(而不是通过闭包)传递所需的上下文数据结构解决了这个问题。

总而言之,触发异常的用例是:

import multiprocessing as mp
context = some_object
pool = mp.Pool()
worker_func = lambda x: work(x, context)
results = pool.map(worker_func, data_list)

关于python - 无法 pickle <type 'function' > : attribute lookup __builtin__. 函数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10898268/

相关文章:

python - Django,仅从相关对象中获取唯一值

python - 如何在 python 中的 pickle.load() 之后关闭文件

python - 当 multiprocessing 使用 fork 作为起始方法时,为什么传递给 Pool.map 的函数会被 pickle ?

python - 找不到马克

Python - 如何仅使用变量使用 dict() 函数?

python - ravendb python api,查询总是返回128

python - 在 nginx 上运行 django 时提供测试页面

python - 如何禁用在标签栏中显示特定文件类型的可见性符号?

python - C 结构体的 Python SWIG 包装器中的深度复制

Django - 在 MultipleChoiceField 中预选选项