【发布时间】:2019-03-14 17:35:38
【问题描述】:
我想知道,我是否应该尽量减少 db_session 的使用?让我们考虑这两个等价的例子:
一)
def do_stuff():
with db_session:
task = orm.make_proxy(Task.select().first())
task.mark_started()
...
this_function_might_take_a_long_time(task)
...
with db_session:
task.mark_done()
B)
@db_session
def do_stuff():
task = Task.select().first()
task.mark_started()
commit()
...
this_function_might_take_a_long_time(task)
...
task.mark_done()
通过阅读文档,我可以看出 Pony doesn't encourage 微管理 db_sessions
With this code each of view function you will define will be wrapped with db_session so you should not care about them.
但是,here 这表明打开它可能需要付出代价(编辑:它没有,请阅读答案)
Before sending the first query, Pony gets a database connection from the connection pool.
我是否正确地说,除了 B 之外的任何东西都是过早的优化,而 A 应该只在有限的数据库连接数场景中考虑?
【问题讨论】:
标签: python orm database-connection ponyorm premature-optimization