[storm] Consistent multithreaded operations
Gerdus van Zyl
gerdusvanzyl at gmail.com
Thu Sep 4 18:02:15 BST 2008
The code as you have it in the first version is not thread safe since
it relies on shared state hence when you bring in locks in the second
version it becomes thread safe.
The solution I have seen to a similar problem is to have a transaction
table and then just to have summary queries/tables to get a balance
for the account. Depends on what you want to do. If you can give
further details/scenario that might lead to a better solution.
~Gerdus
On Thu, Sep 4, 2008 at 6:18 PM, NeedBenzyn NeedBenzyn
<needbenzyn at gmail.com> 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.
>
> The better way I found to do it is something which i believe is not elegant
> and not efficient :
>
> def bankOperation():
>
> store.execute("LOCK TABLE bankaccount WRITE")
>
> account = store.find(BankAccount,BankAccount.id == 1).one()
>
> if account == None:
> store.execute("UNLOCK TABLES")
> return
>
> #Simulate some processing
> waittime = random.random()*4
> time.sleep(waittime)
>
> account.value = account.value + 100
>
> store.flush()
> store.commit()
>
> store.execute("UNLOCK TABLES")
>
>
>
> Any help would be greatly appreciated.
>
> Lucas.
>
>
> --
> storm mailing list
> storm at lists.canonical.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/storm
>
>
More information about the storm
mailing list