我在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
谢谢!
答案 0 :(得分:0)
基于回溯,这看起来更像是一个Python weakrefset
问题,collections.Iterator
依赖于abc
(抽象基类)模块。