绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
python代码写龙卷风_python – 带有龙卷风的ZODB
2022-05-05 16:30:29

我有一个用

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显示.

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

分享好友

分享这个小栈给你的朋友们,一起进步吧。

ZODB
创建时间:2022-04-18 13:36:36
ZODB
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • 飘絮絮絮丶
    专家
戳我,来吐槽~