0

我已经将带有多线程模块的 django 应用程序部署到 dotcloud。该线程将一些缓慢的东西放入数据库(通常需要大约 3-5 秒)。

当我在我的虚拟环境中测试它时 - 一切正常。

这是我的 dotcloud.yml:

www:
  type: python
  config:
    python_version: v2.7
    uwsgi_enable_threads: true
db:
  type: postgresql
data:
  type: mongodb

这是我的点云日志:

[www.0] Process Process-1:
[www.0] Traceback (most recent call last):
[www.0]   File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
[www.0]     self.run()
[www.0]   File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
[www.0]     self._target(*self._args, **self._kwargs)
[www.0]   File "/home/dotcloud/current/usn/views.py", line 416, in sync_countdown
[www.0]     put_current_data_to_db(user_name)
[www.0]   File "/home/dotcloud/current/usn/views.py", line 429, in put_current_data_to_db
[www.0]     current_client = Client.objects.get(user_name=user_name)
[www.0]   File "/home/dotcloud/env/local/lib/python2.7/site-packages/django/db/models/manager.py", line 131, in get
[www.0]     return self.get_query_set().get(*args, **kwargs)
[www.0]   File "/home/dotcloud/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 361, in get
[www.0]     num = len(clone)
[www.0]   File "/home/dotcloud/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 85, in __len__
[www.0]     self._result_cache = list(self.iterator())
[www.0]   File "/home/dotcloud/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 291, in iterator
[www.0]     for row in compiler.results_iter():
[www.0]   File "/home/dotcloud/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 763, in results_iter
[www.0]     for rows in self.execute_sql(MULTI):
[www.0]   File "/home/dotcloud/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
[www.0]     cursor.execute(sql, params)
[www.0]   File "/home/dotcloud/env/local/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in execute
[www.0]     return self.cursor.execute(sql, params)
[www.0]   File "/home/dotcloud/env/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 52, in execute
[www.0]     return self.cursor.execute(query, args)
[www.0] DatabaseError: server closed the connection unexpectedly
[www.0]     This probably means the server terminated abnormally
[www.0]     before or while processing the request.
4

1 回答 1

1

解决 django 线程中数据库问题的常用方法对我有用 - 我刚刚添加了:

django.db.connection.close()

在开始我的话题之前

简短说明:

然后 django 分叉新线程,子进程使用与 DB相同的连接,作为父进程。

如果我们关闭连接,孩子将创建一个新的。

于 2013-01-17T01:02:22.740 回答