我有一些使用多重处理的功能(save_histoty)。 该函数将对象插入数据库。为了改善时间,我使用了来自多处理的过程。 方法运行:
def run(self):
from time import sleep
from django import db
db.connections.close_all()
我对每个进程都使用新的连接,这是必要的。 该功能在night_scripts模块中。而且它的工作很好
但是我使用py.test
def test_order(self):
res = night_scripts.run_night_scripts()
追踪:
def create_cursor(self):
cursor = self.connection.cursor()
E InterfaceError:连接已关闭
CRITICAL [NIGHT TESTING] script <save_history> failed
Traceback (most recent call last):
File "/home/healfy/PycharmProjects/finance_/finance/finance/../apps/main/night_scripts.py", line 65, in wrapper
func(*args, **kwargs)
File "/home/healfy/PycharmProjects/finance_/finance/finance/../apps/main/night_scripts.py", line 268, in delete_unfinished_loans
Loan.objects.delete_unfinished_loans()
File "/home/healfy/PycharmProjects/finance_/finance/finance/../apps/main/managers.py", line 594, in delete_unfinished_loans
.update(status=Loan.REVOKED)
File "/home/healfy/.virtualenvs/finance/local/lib/python2.7/site-packages/django/db/models/query.py", line 637, in update
rows = query.get_compiler(self.db).execute_sql(CURSOR)
File "/home/healfy/.virtualenvs/finance/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1148, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/home/healfy/.virtualenvs/finance/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 833, in execute_sql
cursor = self.connection.cursor()
File "/home/healfy/.virtualenvs/finance/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 233, in cursor
cursor = self.make_cursor(self._cursor())
File "/home/healfy/.virtualenvs/finance/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 206, in _cursor
return self.create_cursor()
File "/home/healfy/.virtualenvs/finance/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/healfy/.virtualenvs/finance/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 206, in _cursor
return self.create_cursor()
File "/home/healfy/.virtualenvs/finance/local/lib/python2.7/site-packages/django/db/backends/postgresql/base.py", line 211, in create_cursor
cursor = self.connection.cursor()
InterfaceError: connection already closed
那意味着当功能完成时,它会关闭所有连接吗?
以及如何解决?
平台linux2 -- Python 2.7.15rc1, pytest-2.9.1, py-1.6.0, pluggy-0.3.1