-1

I have a server, which creates a "AnyEvent timer" watcher object on every client connection (adds it to an AnyEvent loop).

use AnyEvent;

...

my $db_handle = myschema->connect();

my $w; $w = AnyEvent->timer (
       interval => $interval,
       after    => $interval,
       cb => sub {
          ## Remove from loop on some condition
          unless ( $ret = _check_session($sid) ) {
             undef $w;
          }

          ## Execute main logic
          $db_handle->lookup($sid);
       }
);

...

So the callback will be executed every $interval seconds. In case there are a lot of clients, some callbacks will have to be executed at the same time. How does AnyEvent handle this? Does it execute those callbacks one after another OR there is some concurrency mechanism in this case, so that those callbacks will be executed simultaneously? (for instance to speed up the execution of several callbacks that have to be executed at the same time by means of creating several threads)
In case of my server the callback performs database lookup. The database handle for the database connection has been initialized outside of event loop. My concern is that if there is any concurrency mechanism in AnyEvent, then the callbacks cannot be executed simultaneously because one callback has to wait until another one has finished database lookup and the database handle is free.
P.S. Thanks to 'ikegami' for the answer.

  • It was a polite thank you for your participation. The answer is not answered IMHO, you could be more specific about the problems raised in the question. And I think it's not polite to comment like this when someone thanked you (especially after your first reaction was to write a comment, not an answer, i.e. teach yourself first). – Oleksii Andreiev Jan 15 '14 at 20:48
  • ah, hadn't noticed that part of the update. – ikegami Jan 15 '14 at 20:58
  • There's nothing of substance added to the question. The only mention of a problem is that there would be one if there was concurrency, so I don't know how to be more specific then saying there is no concurrency. – ikegami Jan 15 '14 at 21:20
  • I'm not sure what's impolite about pointing out to a newcomer to the site how the site operates when he fails to follow desired behaviour. (1. Including thanks in questions is undesired. 2. A question should be marked as answered using the appropriate tool, not by thanking a user for his answer.) – ikegami Jan 15 '14 at 21:23
  • Ok, I get it, you have never heard about good manners... stop adding your comments – Oleksii Andreiev Jan 16 '14 at 08:19
  • So what would be a good way of instructing you, then? – ikegami Jan 16 '14 at 12:12

2 Answers2

3

AnyEvent does not create any concurrency. It processes events when you call into it (e.g. ->recv).

ikegami
  • 367,544
  • 15
  • 269
  • 518
0

As ikegami said - there's no concurrency in AnyEvent. Any event loop gives you the ability to handle events 'asynchronously' - so you can have multiple requests/operations/timers in progress simultaneously, without ever having concurrent code execution. Events are always handled sequentially.

For your specific case of multiple timers expiring at the same time - each timer is processed in turn (they are sorted on their expiry time), and all 'expired' timers are processed at each event loop iteration - e.g. lines 208-213 of Loop.pm for the pure Perl implementation which you can view @ CPAN.

paulw1128
  • 454
  • 3
  • 14