Idea on subroutine reentrancy and database transaction

Michael Fung mike at 3open.org
Tue Apr 9 04:25:32 CEST 2013


Many thanks Marc!


On 4/9/2013 6:07 AM, Marc Lehmann wrote:
>
>     our $save_transaction = new Coro::Semaphore;
>
>     sub do_save_to_db {
>       my $data = shift;
>
>       my $guard = $save_transaction->guard;
>
>       # begin a db transaction to save $data to redis db
>       # check for some criteria
>       # commit if ok, else do rollback logic
>     }
>
> That will prevent other threads from proceeding through the guard line
> when any thread executes the rest.


Silly me, I forgot mutex and semaphore that I learnt from thread 
programming. That's just what I wanted. But I prefer to use an AnyEvent 
approach so that no need to load Coro:

[code]
use AnyEvent::Tools qw(mutex);

my $mutex_redis = mutex;

my $redis = AnyEvent::Redis::RipeRedis->new( ... );

# a callback sub to do transaction
sub save_to_db {

   $mutex_redis->lock( sub {
     my $guard = shift;

     # do my redis transaction here with $redis
     # ...

     # finally unlock:
     undef $guard;
     });
}
[/code]


I did read the Coro docs last time per your suggestion but found the 
concept a bit hard to understand.


Thanks and regards,
Michael





More information about the anyevent mailing list