Django OperationalError:无法为连接分叉新进程

时间:2012-12-23 19:43:31

标签: django postgresql heroku out-of-memory

我今天早上开始在生产环境中收到此错误,因为Django-storages,Boto和Django-compressor昨天在S3上放置静态文件,虽然我不知道这是否相关..

OperationalError: could not fork new process for connection: Cannot allocate memory

could not fork new process for connection: Cannot allocate memory
could not fork new process for connection: Cannot allocate memory


Stacktrace (most recent call last):

  File "django/core/handlers/base.py", line 89, in get_response
    response = middleware_method(request)
  File "reversion/middleware.py", line 17, in process_request
    if hasattr(request, "user") and request.user.is_authenticated():
  File "django/utils/functional.py", line 184, in inner
    self._setup()
  File "django/utils/functional.py", line 248, in _setup
    self._wrapped = self._setupfunc()
  File "django/contrib/auth/middleware.py", line 16, in <lambda>
    request.user = SimpleLazyObject(lambda: get_user(request))
  File "django/contrib/auth/middleware.py", line 8, in get_user
    request._cached_user = auth.get_user(request)
  File "django/contrib/auth/__init__.py", line 98, in get_user
    user_id = request.session[SESSION_KEY]
  File "django/contrib/sessions/backends/base.py", line 39, in __getitem__
    return self._session[key]
  File "django/contrib/sessions/backends/base.py", line 165, in _get_session
    self._session_cache = self.load()
  File "django/contrib/sessions/backends/db.py", line 19, in load
    expire_date__gt=timezone.now()
  File "django/db/models/manager.py", line 131, in get
    return self.get_query_set().get(*args, **kwargs)
  File "django/db/models/query.py", line 361, in get
    num = len(clone)
  File "django/db/models/query.py", line 85, in __len__
    self._result_cache = list(self.iterator())
  File "django/db/models/query.py", line 291, in iterator
    for row in compiler.results_iter():
  File "django/db/models/sql/compiler.py", line 763, in results_iter
    for rows in self.execute_sql(MULTI):
  File "django/db/models/sql/compiler.py", line 817, in execute_sql
    cursor = self.connection.cursor()
  File "django/db/backends/__init__.py", line 308, in cursor
    cursor = util.CursorWrapper(self._cursor(), self)
  File "django/db/backends/postgresql_psycopg2/base.py", line 177, in _cursor
    self.connection = Database.connect(**conn_params)
  File "psycopg2/__init__.py", line 178, in connect
    return _connect(dsn, connection_factory=connection_factory, async=async)

我正在Heroku上部署该网站。重新启动应用程序后它会工作一段时间,但几分钟后会再次停止工作。

关于可能导致这种情况的任何想法?

1 个答案:

答案 0 :(得分:3)

我在heroku上尝试使用postgresql数据库设置一个简单的django Web应用程序并设法解决它时遇到了同样的问题。

我不完全理解错误,但修复非常简单:当您将查询创建的python列表传递到数据库时,需要 限制列表的大小

因此,例如,如果您作为上下文传递以下列表:

set_list = userSetTable.objects.all()

return render(request, 'fc/user.html', {'set_list': set_list,})

这会导致错误,因为set_list可能非常大。您需要指定最大尺寸:

set_list = userSetTable.objects.all()[0:20]

因此,在实际应用程序中,您可能希望将列表显示为页面结果或其他......您明白了。