Function is running multiple time as per CPU core(CPU cores = 4 in my case). How can I run function only 1 time?

Nishant Khandelwal

In my Node js application due to clustering, the function is executing multiple time. How can I run a function only 1 time? Actually, want to run cron job but due to multiple execution of the function, cron is executing multiple time.

Clustering code: `

var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
    // Fork workers.
    // console.log('numCPUs--------',numCPUs);
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
        console.log(process.pid);
    }
    cluster.on('exit', function (worker, code, signal) {
        // handle server crashes
        console.log('worker ' + worker.process.pid + ' died');
    });
} else {
    console.log(cluster.worker.id);
    const port = process.env.PORT || 8000;
    app.listen(8000);
}
(function test() {
    console.log("THIS is test");
}());


OUTPUT: 4
THIS is test
1
3
THIS is test
THIS is test
2
THIS is test

`

Samir Aleido

Move the call to test function to master block

var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
    test(); // calling test function moved to here
    // Fork workers.
    // console.log('numCPUs--------',numCPUs);
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
        console.log(process.pid);
    }
    cluster.on('exit', function (worker, code, signal) {
        // handle server crashes
        console.log('worker ' + worker.process.pid + ' died');
    });
} else {
    console.log(cluster.worker.id);
    const port = process.env.PORT || 8000;
    app.listen(8000);
}

// implementing the function only
function test () {
    console.log("THIS is test");
}

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 can I improve the run-time complexity of my method?

From Dev

How to measure CPU running time and wall clock running time of a function, separately, as Python code (not from terminal)?

From Dev

How can I make my function run in constant time?

From Dev

how can I pass an argument to a function handle, but interpret it only on call time?

From Dev

In JavaScript, how can I have a function run at a specific time?

From Dev

How do I run a custom function ONLY the first time a user scrolls to a certain element?

From Dev

How can i activate .hover() function only on one element at time

From Dev

How can I make my function to run the moment the page is running?

From Dev

How can I run a macro as a workbook opens for the first time only?

From Dev

Trying to figure out the run time of my function

From Dev

How would I calculate the worst case time complexity of this recursive function?

From Dev

How can I ensure my cronjob will run at specified time?

From Dev

How can I improve the run-time complexity of my method?

From Dev

How can I ensure my cronjob will run at specified time?

From Dev

Can I run my CPU at 100% usage for a long time?

From Dev

How to run "time" on a function in zsh

From Dev

How do I calculate the worst-case (theoretical) running time of this recursive function?

From Dev

how to populate data at run time in my case?

From Dev

Openerp - How can i call a function to run every time a specific view is called?

From Dev

How can I run function for each element in different time?

From Dev

How can I run a macro as a workbook opens for the first time only?

From Dev

How do I run a function until time is multiple of 5 seconds in Perl?

From Dev

How can I call function 1 time only with Android service?

From Dev

How can I use my local time as the only reference of `ntpd`?

From Dev

How to get CPU time used by a function in Swift

From Dev

Run the function only once at the same time

From Dev

Why is my regex function only running the first time around?

From Dev

How can I run a function every specific time within a while cycle on python?

From Dev

My matplotlib bar-chart duplicates every time a function is run, how can I refresh my bar-chart?

Related Related

  1. 1

    How can I improve the run-time complexity of my method?

  2. 2

    How to measure CPU running time and wall clock running time of a function, separately, as Python code (not from terminal)?

  3. 3

    How can I make my function run in constant time?

  4. 4

    how can I pass an argument to a function handle, but interpret it only on call time?

  5. 5

    In JavaScript, how can I have a function run at a specific time?

  6. 6

    How do I run a custom function ONLY the first time a user scrolls to a certain element?

  7. 7

    How can i activate .hover() function only on one element at time

  8. 8

    How can I make my function to run the moment the page is running?

  9. 9

    How can I run a macro as a workbook opens for the first time only?

  10. 10

    Trying to figure out the run time of my function

  11. 11

    How would I calculate the worst case time complexity of this recursive function?

  12. 12

    How can I ensure my cronjob will run at specified time?

  13. 13

    How can I improve the run-time complexity of my method?

  14. 14

    How can I ensure my cronjob will run at specified time?

  15. 15

    Can I run my CPU at 100% usage for a long time?

  16. 16

    How to run "time" on a function in zsh

  17. 17

    How do I calculate the worst-case (theoretical) running time of this recursive function?

  18. 18

    how to populate data at run time in my case?

  19. 19

    Openerp - How can i call a function to run every time a specific view is called?

  20. 20

    How can I run function for each element in different time?

  21. 21

    How can I run a macro as a workbook opens for the first time only?

  22. 22

    How do I run a function until time is multiple of 5 seconds in Perl?

  23. 23

    How can I call function 1 time only with Android service?

  24. 24

    How can I use my local time as the only reference of `ntpd`?

  25. 25

    How to get CPU time used by a function in Swift

  26. 26

    Run the function only once at the same time

  27. 27

    Why is my regex function only running the first time around?

  28. 28

    How can I run a function every specific time within a while cycle on python?

  29. 29

    My matplotlib bar-chart duplicates every time a function is run, how can I refresh my bar-chart?

HotTag

Archive