ITPub博客

首页 > 数据库 > > python代码写龙卷风_python – 带有龙卷风的ZODB

python代码写龙卷风_python – 带有龙卷风的ZODB

作者:gaokeke123 时间:2022-05-05 16:30:29 0 删除 编辑

我有一个用

Tornado构建的小型网络应用程序,我想使用

ZODB进行一些数据存储.根据ZODB文档,

multi-threaded programs are supported, but they should start up a new connection per thread.我认为这意味着我必须做类似的事情

### On startup

dbFilename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Data.fs")

db = DB(FileStorage(dbFilename))

### Example handler

class Example(tornado.web.RequestHandler):

def get(self):

try:

conn = db.open()

root = conn.root()

### do stuff with root here

root._p_changed = 1 ## Include these lines for writes

transaction.commit() ## on sub-elements

finally:

conn.close()

首先,对于所有与数据库交互的处理程序或仅进行写入的处理程序,新连接是否仍然是必需的?在启动时启动一个连接并将其用于我的所有读取是否合理,然后只在我需要写东西时才进行上述连接歌曲和舞蹈?

其次,在Python中抽象该模式的惯用方法是什么?我有类似的东西

def withDB(fn):

try:

conn = db.open()

root = conn.root()

res = fn(root)

root._p_changed = 1

transaction.commit()

return res

finally:

conn.close()

def delete(formName):

def local(root):

### do stuff with root here

return withDB(local)

记住,但这可能是我的Lisp显示.

对这种方法进行一般性的核查也是值得欢迎的.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26692020/viewspace-3138067/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
大家好

注册时间:2013-08-14

  • 博文量
    1432
  • 访问量
    31659