我正在尝试使用 gevent 在 gunicorn 服务器上运行我的 Django 应用程序。在我的 gunicorn.conf 中,我修改了所有内容:
def on_starting(server):
from gevent import monkey
monkey.patch_socket()
def post_fork(server, worker):
import pymysql
pymysql.install_as_MySQLdb()
更新:我使用 Django 1.5 和 pymysql 0.5
现在,当我运行 Django 时,我得到以下异常:
ImportError at /
Exception Value: cannot import name SERVER_STATUS
Exception Location: /var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/pymysql/connections.py in <module>, line 37
这是回溯:
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
92. response = middleware_method(request)
File "/var/www/myproject/myproject/myapp/middleware.py" in process_request
91. if not request.session.get('language_code', False):
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in get
57. return self._session.get(key, default)
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in _get_session
168. self._session_cache = self.load()
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py" in load
18. expire_date__gt=timezone.now()
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/manager.py" in get
143. return self.get_query_set().get(*args, **kwargs)
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/query.py" in get
382. num = len(clone)
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/query.py" in __len__
90. self._result_cache = list(self.iterator())
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/query.py" in iterator
301. for row in compiler.results_iter():
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
775. for rows in self.execute_sql(MULTI):
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
839. cursor = self.connection.cursor()
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/backends/__init__.py" in cursor
324. cursor = self.make_debug_cursor(self._cursor())
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py" in _cursor
405. self.connection = Database.connect(**kwargs)
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/MySQLdb/__init__.py" in Connect
80. from MySQLdb.connections import Connection
File "/var/www/virtualenvs/myproject/local/lib/python2.7/site-packages/pymysql/connections.py" in <module>
37. from constants import SERVER_STATUS
我不明白为什么我不能在这里导入 SERVER_STATUS。这个文件存在并且在“python manage.py shell”里面我可以导入这个文件。即“从 pymysql.constants 导入 SERVER_STATUS”有效。
有什么建议吗?我可以想象这是一个非常愚蠢的错误......
提前致谢!