我在我认为与缓存有关的 django 项目中遇到了这个问题。它每 6 个月左右出现一次,并通过重新启动应用程序“修复”,但几个月后才会出现。突然之间,所有视图都会因此异常而失败。
TypeError: weak object has gone away
而且它并不总是同一个对象,每个视图都因尝试访问不同的对象而失败。在这种情况下,它正在访问用户(使用request.user.is_authenticated()
),但其他页面使用其他对象。
我认为缓存一定有问题。甚至可能它在 django 中也是一个问题。我不知道。它工作得很好,直到它没有,而且似乎没有人在互联网上谈论这个例外。
这是一个示例回溯:
Traceback (most recent call last):
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/core/handlers/base.py", line 88, in get_response
response = middleware_method(request)
File "/home/rsaenz/webapps/club/club/cms/socios/views.py", line 143, in process_request
if not request.user.is_authenticated():
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/utils/functional.py", line 213, in inner
self._setup()
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/utils/functional.py", line 298, in _setup
self._wrapped = self._setupfunc()
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 18, in <lambda>
request.user = SimpleLazyObject(lambda: get_user(request))
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 10, in get_user
request._cached_user = auth.get_user(request)
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 140, in get_user
user_id = request.session[SESSION_KEY]
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py", line 47, in __getitem__
return self._session[key]
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py", line 173, in _get_session
self._session_cache = self.load()
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py", line 20, in load
expire_date__gt=timezone.now()
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/manager.py", line 151, in get
return self.get_queryset().get(*args, **kwargs)
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/query.py", line 301, in get
clone = self.filter(*args, **kwargs)
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/query.py", line 593, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/query.py", line 611, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1204, in add_q
clause = self._add_q(where_part, used_aliases)
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1240, in _add_q
current_negated=current_negated)
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1131, in build_filter
clause.add(constraint, AND)
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/utils/tree.py", line 104, in add
data = self._prepare_data(data)
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/db/models/sql/where.py", line 61, in _prepare_data
if is_iterator(value):
File "/home/rsaenz/virtualenvs/club/lib/python2.7/site-packages/django/utils/itercompat.py", line 30, in is_iterator
return isinstance(x, collections.Iterator)
File "/home/rsaenz/virtualenvs/club/lib/python2.7/abc.py", line 144, in __instancecheck__
return cls.__subclasscheck__(subtype)
File "/home/rsaenz/virtualenvs/club/lib/python2.7/abc.py", line 184, in __subclasscheck__
cls._abc_negative_cache.add(subclass)
File "/home/rsaenz/virtualenvs/club/lib/python2.7/_weakrefset.py", line 86, in add
self.data.add(ref(item, self._remove))
TypeError: weak object has gone away
谢谢!