cron作业是否阻塞了主进程,或者nodejs将创建一个工作程序来执行cron任务

问题描述:

我正在使用 node-cron 每分钟执行一些繁重的任务(更新数据库).该任务是否使用主流程来工作,或者nodejs将创建一些工作人员来完成这些任务?

I am using node-cron to do some heavy tasks (update database) every minute. Does this task use main process to work or nodejs will create some workers to do these taks?

var CronJob = require('cron').CronJob;
new CronJob('0 * * * * *', function() {
  //Update database every minute here
  console.log('Update database every minute');
}, null, true, 'America/Los_Angeles');

应该为您创建一个工作程序..在库文档中没有很好的文档说明,但: 1)您可以在依赖项中看到它,它取决于node-worker. 2)如果cron作业要阻塞,那么等待cron作业执行(在本例中为一分钟)也将阻塞.这是因为主线程只会等待直到必须执行它.在这种情况下,它将不是cron作业,因为它将是一个简单的sleep()然后执行.

It is supposed to create a worker for you.. It is not well documented in the library docs but: 1) You can see at the dependencies, it depends on node-worker. 2) If the cron job were to be blocking, then the waiting for the cron job to execute (in this case, a minute) would be blocking as well. This is because the main thread will just wait until it has to do it. Which in this case, it will be no cron job because it will be a simple sleep() and then execute.

但是,如果您想确定的话,请尝试使用"while true"并在内部编写一些要控制台的东西来编写一个nodejs主程序.并进行一次cronjob,使其每分钟都会在您希望的时间执行sleep()命令.预期的症状是控制台中的写操作永远都不应停止..

Although, if you want to be sure, try doing a nodejs main program with a "while true" and inside probably writing something to console. And make a cronjob that every minute it will execute a sleep() command for the time you wish. The expected symptom is that the writing in console should never stop ..

希望这会有所帮助. 干杯

Hope this helps.. Cheers