Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^2: AnyEvent: How to protect critical sections?

by saintmike (Vicar)
on May 17, 2011 at 20:37 UTC ( #905358=note: print w/replies, xml ) Need Help??

in reply to Re: AnyEvent: How to protect critical sections?
in thread AnyEvent: How to protect critical sections?

I understand, but the problem is that I have two competing tasks: One is the timer firing, the other is a http request callback having data available.

I simply don't want the timer callback to be executed while the http request is under way, but *right after*.

I guess I could just restart the timer and try to execute the interrupt later, but that's a) non-deterministic because it could fail again and b) not very efficient because I'd like to run it as soon as the http request has been completed.

  • Comment on Re^2: AnyEvent: How to protect critical sections?

Replies are listed 'Best First'.
Re^3: AnyEvent: How to protect critical sections?
by Corion (Patriarch) on May 17, 2011 at 20:43 UTC

    Then I would cancel the timer when starting the HTTP request, and relaunch the timer in the HTTP request on_body callback (and likely in the on_error callback too). You can "immediately" launch a timer by launching it with after => 0 - this will launch the timer callback the next time the event loop is entered.

      Nice idea, that could actually work, I'll try that in a minute.

      I was just hoping that there was some kind of $condvar->send/recv mechanism that supported lock()/unlock() so that I could write something like

      $condvar->lock(); http_get "http://blah", sub { $condvar->unlock(); print $_[1] };

      sub timer_callback { $condvar->lock(); # ... $condvar->unlock(); }

        This cannot work - if you expect ->lock() to block until the semaphore is released, how can it continue to execute the remaining code elsewhere?

        I would either use a plain boolean variable, like you did in your first case, and keep the timer running:

        my $critical; sub on_timer { if (! $critical) { ... }; };

        ... or manually restart the timer when it's allowed to restart again. The guard values returned from creating a timer allow you to conveniently stop a timer.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://905358]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2022-08-08 02:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found