JavaScript Worker : how to check if a message was received while running expensive task

Olivier

I have a very expensive task running on a Worker, similar as this

for(var i = 0; i < 1000000000; i++)
    //operations using i...

How can I make is so that, in that loop, I can check if a message was received from the Worker owner asking it to stop? I would like to have something like this

for(var i = 0; i < 1000000000; i++)
    if(windowDidNotAskToStop())
        //operations using i...

Right now I have a onmessage function registered so I can start listen to message coming from the owner, but it is blocked while my loop is running (obviously).

I imagine that the postMessage calls from the owner are queued somewhere, so I would simply have to access that in order to process the calls from inside my loop.

Ry-

You’ll have to handle the events as usual and set a flag, but make sure to leave time for the event to be received in your loop, probably using setTimeout:

var exitLoop = false;

(function loop(i) {
    if (exitLoop || i >= 1000000000) {
        return;
    }

    // …

    setTimeout(function () {
        loop(i + 1);
    }, 0);
})(0);

onmessage = function (e) {
    if (e.data === 'stop') {
        exitLoop = true;
    }
};

Or, as a general utility function:

function asyncIterateRange(start, end, callback) {
    var exitLoop = false;
    var doneCallbacks = [];

    (function loop(i) {
        if (exitLoop) {
            return;
        }

        if (i >= end) {
            doneCallbacks.forEach(function (callback) {
                callback();
            });

            return;
        }

        callback(function () {
            setTimeout(function () {
                loop(i + 1);
            }, 0);
        });
    })(start);

    return {
        abort: function abort() {
            exitLoop = true;
        },
        done: function addDoneCallback(callback) {
            doneCallbacks.push(callback);
        }
    };
}

Use it like so:

var innerIterator;
var outerIterator = asyncIterateRange(0, 1000000, function outerLoop(next) {
    innerIterator = asyncIterateRange(0, 1000, function innerLoop(next) {
        // …
        next();
    }).done(next);
});

// To stop:
if (innerIterator) {
    innerIterator.abort();
}

outerIterator.abort();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to pause task running on a worker thread and wait for user input?

From Dev

Celery: Worker with concurrency and reserved tasks only running 1 task

From Dev

how to check received message is read or unread in xmpp framework in ios

From Dev

Python- How to check if program gets aborted by user while running?

From Dev

Show message while javascript is running

From Dev

How to run a custom task(functional tests written in protractor) while the "run" task is up and running?

From Dev

How to check if previous task is still running and stop/cancel it?

From Dev

How to check if Worker Role is running in Azure Emulator

From Dev

running a "background task" in javascript

From Dev

Celery & RabbitMQ running as docker containers: Received unregistered task of type '...'

From Dev

How can I convert bytes received in an AWS error message into something readable in Javascript?

From Dev

Is re-initializing a javascript array expensive enough to add a check condition?

From Dev

How to check if there is any background task running?

From Dev

Push notifications disappear if received while app not running

From Dev

masstransit filter received message while consume

From Dev

Firebase message not received while using api

From Dev

How to turn off the filesystem check message which occures while booting

From Dev

How to check worker status in Asyncio?

From Dev

How to turn off the filesystem check message which occures while booting

From Dev

How to check 'mdadm' RAIDs while running?

From Dev

javascript check setTimeout is running

From Dev

Show message while javascript is running

From Dev

Run task while application is running

From Dev

Check if there is a message received or skip it (python 3.4)

From Dev

How can I convert bytes received in an AWS error message into something readable in Javascript?

From Dev

Background worker freezes / dies while running

From Dev

How to Update UI while a task is running in another thread in android?

From Dev

Celery task not received by worker when two projects

From Dev

Scrolling while message box is running

Related Related

  1. 1

    How to pause task running on a worker thread and wait for user input?

  2. 2

    Celery: Worker with concurrency and reserved tasks only running 1 task

  3. 3

    how to check received message is read or unread in xmpp framework in ios

  4. 4

    Python- How to check if program gets aborted by user while running?

  5. 5

    Show message while javascript is running

  6. 6

    How to run a custom task(functional tests written in protractor) while the "run" task is up and running?

  7. 7

    How to check if previous task is still running and stop/cancel it?

  8. 8

    How to check if Worker Role is running in Azure Emulator

  9. 9

    running a "background task" in javascript

  10. 10

    Celery & RabbitMQ running as docker containers: Received unregistered task of type '...'

  11. 11

    How can I convert bytes received in an AWS error message into something readable in Javascript?

  12. 12

    Is re-initializing a javascript array expensive enough to add a check condition?

  13. 13

    How to check if there is any background task running?

  14. 14

    Push notifications disappear if received while app not running

  15. 15

    masstransit filter received message while consume

  16. 16

    Firebase message not received while using api

  17. 17

    How to turn off the filesystem check message which occures while booting

  18. 18

    How to check worker status in Asyncio?

  19. 19

    How to turn off the filesystem check message which occures while booting

  20. 20

    How to check 'mdadm' RAIDs while running?

  21. 21

    javascript check setTimeout is running

  22. 22

    Show message while javascript is running

  23. 23

    Run task while application is running

  24. 24

    Check if there is a message received or skip it (python 3.4)

  25. 25

    How can I convert bytes received in an AWS error message into something readable in Javascript?

  26. 26

    Background worker freezes / dies while running

  27. 27

    How to Update UI while a task is running in another thread in android?

  28. 28

    Celery task not received by worker when two projects

  29. 29

    Scrolling while message box is running

HotTag

Archive