我已经部署了一个django应用程序与多线程模块到dotcloud。这个线程把一些慢的东西放到DB(通常需要3-5秒)。
当我在虚拟环境中测试时-一切正常。
这是我的。cloud.yml:
www:
type: python
config:
python_version: v2.7
uwsgi_enable_threads: true
db:
type: postgresql
data:
type: mongodb
这是我的dotcloud日志:
[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.
解决django线程中数据库问题的常用方法-我刚刚添加了:
django.db.connection.close()
开始我的线程
简短说明:
然后django分叉新的线程,子线程使用与父线程相同的连接到DB。
如果我们关闭连接,子节点将创建一个新的。