在Django 1.8中手动提交 [英] Manual commit in Django 1.8
问题描述
如何在Django 1.8中实现 @commit_manually
我正在尝试升级Django 1.5代码与Django 1.8一起工作,出于一些奇怪的原因,Django 1.6中没有直接替换, c> commit_manually 装饰器已被删除。我的进程遍历数千条记录,所以它不能将整个进程包装在单个事务中,而不会耗尽内存,但它仍然需要在事务中分组一些记录以提高性能。为了做到这一点,我有一个包含@commit_manually的方法,每次N次迭代都调用transaction.commit()。
我从 docs ,但这似乎仍然受支持。我只需要调用 set_autocommit(False)
而不是方便的装饰器。这是否正确?
是的,你已经有了。调用 set_autocommit(False)
开始一个事务,然后调用 commit()
和 set_autocommit
您可以将其包装在自己的装饰器中:
set_autocommit(False)
res = fn(*)
def _commit_manually(* args,** kwargs) args,** kwargs)
commit()
set_autocommit(True)
return res
return _commit_manually
How do you implement @commit_manually
in Django 1.8?
I'm trying to upgrade Django 1.5 code to work with Django 1.8, and for some bizarre reason, the commit_manually
decorator was removed in Django 1.6 with no direct replacement. My process iterates over thousands of records, so it can't wrap the entire process in a single transaction without running out of memory, but it still needs to group some records in a transaction to improve performance. To do this, I had a method wrapped with @commit_manually, which called transaction.commit() every N iterations.
I can't tell for sure from the docs, but this still seems supported. I just have to call set_autocommit(False)
instead of having a convenient decorator. Is this correct?
Yeah, you've got it. Call set_autocommit(False)
to start a transaction, then call commit()
and set_autocommit(True)
to commit it.
You could wrap this up in your own decorator:
def commit_manually(fn):
def _commit_manually(*args, **kwargs):
set_autocommit(False)
res = fn(*args, **kwargs)
commit()
set_autocommit(True)
return res
return _commit_manually
这篇关于在Django 1.8中手动提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!