[storm] using twisted-storm

Drew Smathers drew.smathers at gmail.com
Wed Apr 29 19:42:22 BST 2009


On Fri, Apr 17, 2009 at 6:35 PM, Lee Connell <lee.a.connell at gmail.com> wrote:
> Does anyone have any examples on how to use storm-twisted integration within
> a twisted server?
>

tests/twiststorm.py is about all there is now in terms of
documentation - of course this isn't documentation, but all you need
to use is demonstrated there.  External documentation is something
I've been wanting to contribute actually.

I normally use inlineCallbacks around code using DeferredStore etc.,
so a typical function might look like:

@inlineCallbacks
def dostuff():
     store = DeferredStore(database)
     yield store.start()
     foo = Foo()
     yield store.add(foo)
     ...
     bars = yield (yield store.find(Bar, Bar.type==u'chocolate')).all()
     for bar in bars:
        bar.price += 1
     yield store.commit()

You can also use a store pool implementation with a transact API:

@inlineCallbacks
def tx(store):
    foo = yield store.get(Foo, 1)
    ... do stuff (commit/rollback occurs after return)

def cb_pool(pool):
    return pool.transact(tx)

pool = StorePool(database, min, max)
pool.start().addCallback(lambda whatever : cb_pool(pool))


-Drew



More information about the storm mailing list