Tornado으로 구축 된 작은 웹 응용 프로그램을 가지고 있는데 일부 데이터 저장소에 ZODB을 사용하고 싶습니다. ZODB 문서 (multi-threaded programs are supported, but they should start up a new connection per thread)에 따르면 나는 새로운 연결을 할 쓰기 모든 DB-상호 작용 핸들러하거나 사람을 위해 여전히 필요하다, 즉 내가 먼저ZODB with Tornado
### 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()
같은 것을 할 필요가 있음을 의미한다고 생각? 시작시 하나의 연결을 시작하여 모든 읽기에 사용하는 것이 합리적일까요? 그리고 위의 연결은 내가 뭔가를 쓸 필요가있을 때만 노래와 춤을 할 수 있습니까?
둘째, 패턴을 파이썬에서 추상화하는 관용적 인 방법은 무엇입니까?
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의 내 모습입니다.
접근 방식에 대한 일반적인 점검도 환영합니다.
흠. [this] (http://zodb.readthedocs.org/en/latest/api.html#connection-pool)에 따르면,'ZODB.DB'는 이미'open()'을 통해 풀링 연결을합니다. 이것은'transaction.manager와 m : db.open (transaction_manager = m) ... '같은 것을 할 수 있다는 것을 의미합니까? – Inaimathi
아 물론, 그렇습니다. 나는 나를 위해 자동으로 연결과 트랜잭션을 처리하는 시스템에 버릇이있다. :-) –