setInterval/setTimeout and the event queue

Posted on
  • As I understand it, Espruino internally keeps an event queue, so when timeout or interval timers fire, the appropriate task is added to the queue, and handled in order when the interpreter is next idle.

    So, let us consider a program where we have an interval set to run every 20 milliseconds:

    setInterval("animateLEDs()",20);

    Now, let's say we're also connected to the interwebs, and it has to make an HTTP request from, say, coldmolasses.net , which takes 10 seconds to reply, during which time (as I understand it) everything blocks. Whether HTTP requests still block is immaterial here though - point is, a task that blocks for significantly longer than the 20 millisecond interval.

    What happens to that interval during that time?

    I vaguely recall that this would result in a whole bunch of callbacks on the interval getting queued up.
    Am I correct so far?

    While in some cases this is a good thing, I might not want that.

    So I might do:

    
    setTimeout("onTimeout()",20);
    function onTimeout() {
    animateLEDs();
    setTimeout("onTimeout()",20);
    }
    
    
    

    Am I on the right track here?
    Is my understanding of interval/timeout and the event queue correct, or has my time working with Arduino clouded my brain? (it's such a shock going between "don't approach a String without stakes, holy water and a clove of garlic" and "Strings are fast and efficient, use them whenever you want to")

    Is there a part of the reference that describes how the queue'ed events work (basically, this stuff, and how events from setWatch get queued up)

  • I'm not sure about an Internet request blocking..., because a callback is passed onto the request to handle the response.

    With that said, other things can move on and do their thing.

    Of course not every callback is asynchronous, but why ask for one and then just be sychronous? For a synchronous request, assigning the request result to a variable would be sufficient.

  • Like I said - whether that example actually blocks is beside the point (I think it does, or did recently - at least Original/Pico - I remember being flabbergasted when I realized it, and I think there was a big forum thread here discussing it. Neil Kolban was involved in that one, IIRC - I think he flagged up this behavior while working on ESP8266).

    The question is more about the behavior of intervals and timeouts when there are blocking tasks.

  • PICO and Espruino on ESP8266 are different stories... ESP8266 may block in the non-ESPRUINO part of the firmware while handling the communication... A PICO combined with an ESP8266 leaves the ESP8266 alone and is blocked only when doing some fast bit banging for software rather hardware/device communication, calculation,...

  • The net and http libraries in espruino are not blocking and use callbacks to signal completion or the availability of data. This is also true of the esp8266 port.

  • The only things that block when connecting are WIZnet and CC3000. The ESP8266 (via AT commands or when running on ESP8266 itself) doesn't. After that, nothing blocks at all.

    But yes, if something blocks (let's just say a big FOR loop) then setInterval will try and catch up - but it will do so by calling the interval only once around each idle loop.

    So if you have:

    setInterval(a,20);
    setIntervall(b,40);
    

    You'd expect normally it'd do:

    aabaabaabaabaabaabaabaab...
    

    But when catching up it'd do:

    abababababababab ... now caught up ... aabaabaabaab
    

    And yes, to avoid it, just do a setTimeout when executing - but it won't be as accurate.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

setInterval/setTimeout and the event queue

Posted by Avatar for DrAzzy @DrAzzy

Actions