class'psycopg2.InterfaceError':连接已经关闭

时间:2012-02-24 08:44:48

标签: django postgresql connection fastcgi flup

我正在把头发拉过来,准备好做些坏事。

我正在运行nginx + django + postgresql。有一半时间我试图测试我的网站并打开一个页面,我得到以下内容:

<class 'psycopg2.InterfaceError'>   Python 2.7.2: /home/webapp/newavenue/bin/python
Fri Feb 24 08:28:58 2012
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.

 /home/webapp/newavenue/lib/python2.7/site-packages/flup/server/fcgi_base.py in run(self=<flup.server.fcgi_base.Request object>)
    572             
    573         try:
=>  574             protocolStatus, appStatus = self.server.handler(self)
    575         except:
    576             traceback.print_exc(file=self.stderr)
protocolStatus undefined, appStatus undefined, self = <flup.server.fcgi_base.Request object>, self.server = <flup.server.fcgi_fork.WSGIServer object>, self.server.handler = <bound method WSGIServer.handler of <flup.server.fcgi_fork.WSGIServer object>>
 /home/webapp/newavenue/lib/python2.7/site-packages/flup/server/fcgi_base.py in handler(self=<flup.server.fcgi_fork.WSGIServer object>, req=<flup.server.fcgi_base.Request object>)
   1157         try:
   1158             try:
=> 1159                 result = self.application(environ, start_response)
   1160                 try:
   1161                     for data in result:
result = None, self = <flup.server.fcgi_fork.WSGIServer object>, self.application = <django.core.handlers.wsgi.WSGIHandler object>, environ = {'CONTENT_LENGTH': '', 'CONTENT_TYPE': '', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8', 'HTTP_CACHE_CONTROL': 'max-age=0', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_COOKIE': 'csrftoken=8a0176b368b17af00a8563b5f95b134b; sess...e5bdd0d0a4e3092e7a9abd0029c02; django_language=en', 'HTTP_HOST': 'newavedev.zapto.org', ...}, start_response = <function start_response>
 /home/webapp/newavenue/lib/python2.7/site-packages/django/core/handlers/wsgi.py in __call__(self=<django.core.handlers.wsgi.WSGIHandler object>, environ={'CONTENT_LENGTH': '', 'CONTENT_TYPE': '', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8', 'HTTP_CACHE_CONTROL': 'max-age=0', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_COOKIE': 'csrftoken=8a0176b368b17af00a8563b5f95b134b; sess...e5bdd0d0a4e3092e7a9abd0029c02; django_language=en', 'HTTP_HOST': 'newavedev.zapto.org', ...}, start_response=<function start_response>)
    272                 response = self.get_response(request)
    273         finally:
=>  274             signals.request_finished.send(sender=self.__class__)
    275 
    276         try:
global signals = <module 'django.core.signals' from '/home/webapp...python2.7/site-packages/django/core/signals.pyc'>, signals.request_finished = <django.dispatch.dispatcher.Signal object>, signals.request_finished.send = <bound method Signal.send of <django.dispatch.dispatcher.Signal object>>, sender undefined, self = <django.core.handlers.wsgi.WSGIHandler object>, self.__class__ = <class 'django.core.handlers.wsgi.WSGIHandler'>
 /home/webapp/newavenue/lib/python2.7/site-packages/django/dispatch/dispatcher.py in send(self=<django.dispatch.dispatcher.Signal object>, sender=<class 'django.core.handlers.wsgi.WSGIHandler'>, **named={})
    170 
    171         for receiver in self._live_receivers(_make_id(sender)):
=>  172             response = receiver(signal=self, sender=sender, **named)
    173             responses.append((receiver, response))
    174         return responses
response undefined, receiver = <function close_connection>, signal undefined, self = <django.dispatch.dispatcher.Signal object>, sender = <class 'django.core.handlers.wsgi.WSGIHandler'>, named = {}
 /home/webapp/newavenue/lib/python2.7/site-packages/django/db/__init__.py in close_connection(**kwargs={'sender': <class 'django.core.handlers.wsgi.WSGIHandler'>, 'signal': <django.dispatch.dispatcher.Signal object>})
     83 def close_connection(**kwargs):
     84     for conn in connections.all():
=>   85         conn.close()
     86 signals.request_finished.connect(close_connection)
     87 
conn = <django.db.backends.postgresql_psycopg2.base.DatabaseWrapper object>, conn.close = <bound method DatabaseWrapper.close of <django.d...postgresql_psycopg2.base.DatabaseWrapper object>>
 /home/webapp/newavenue/lib/python2.7/site-packages/django/db/backends/__init__.py in close(self=<django.db.backends.postgresql_psycopg2.base.DatabaseWrapper object>)
    242     def close(self):
    243         if self.connection is not None:
=>  244             self.connection.close()
    245             self.connection = None
    246 
self = <django.db.backends.postgresql_psycopg2.base.DatabaseWrapper object>, self.connection = <connection object at 0x1d37130; dsn: 'dbname=ne...er=postgres host=localhost port=5432', closed: 2>, self.connection.close = <built-in method close of psycopg2._psycopg.connection object>
<class 'psycopg2.InterfaceError'>: connection already closed 
      args = ('connection already closed',) 
      cursor = None 
      message = 'connection already closed' 
      pgcode = None 
      pgerror = None

使用以下命令运行服务器以生成前面的flup回溯报告:

python manage.py runfcgi host=127.0.0.1 port=8000 debug=False

我可以通过修补类似的方式来解决这个问题:“try:self.connection.close(),除了:绕过那条违规行?它可能会工作,但我认为它听起来很脏(搞乱核心),我想尝试寻找替代解决方案。

编辑:考虑这个人的方法:评论出来:http://osdir.com/ml/DjangoUsers/2009-04/msg01647.html

1 个答案:

答案 0 :(得分:2)

我想我解决了这个问题。这不是flup-psycopg2设置的真正解决方案,而是一种替代方案。我从使用fcgi切换到gunicorn,并将self.connection.close()行包装为“try:self.connection.close(),除了:pass”。这也解决了我用nginx遇到的502 Bad Gateway问题。

此外,我的网站似乎运行得更快,这总是一个甜蜜的副作用。