[storm] Consistent multithreaded operations
Jamu Kakar
jkakar at kakar.ca
Thu Sep 4 19:47:13 BST 2008
Hi,
NeedBenzyn NeedBenzyn wrote:
> I'd like to know what would be the proper way in storm to execute that
> kind of code on a multithreaded server (acessing MySql server InnoDb
> tables):
>
> def bankOperation():
>
> account = store.find(BankAccount,BankAccount.id == 1).one()
> if account == None:
> return
>
> #Simulate some processing
> waittime = random.random()*4
> time.sleep(waittime)
>
> account.value = account.value + 100
>
> store.flush()
> store.commit()
>
> ==> If I have more than one thread executing that code data become
> inconsistent.
You need to make sure you use one Store per-thread. Something like
this (untested) code should work:
def bank_operation(store, id):
account = store.find(BankAccount, BankAccount.id == id).one()
if account:
account.value += 100
def run_thread():
store = Store(create_database(...))
try:
bank_operation(store, 1)
except:
store.rollback()
raise
else:
store.commit()
Storm automatically uses transactions, so you shouldn't have to
worry about locking tables and such (though this may not be true for
SQLite).
Thanks,
J.
More information about the storm
mailing list