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

我有一个用

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值