I want to know if the following JS script (server side) is correct or not, because it takes too much CPU resource. The working of cron
job this way.
我想知道以下JS脚本(服务器端)是否正确,因为它占用了太多的CPU资源。以这种方式工作的cron工作。
When server runs the PHP every second (via cron
job), the PHP script (abc123.php) will connect to database and return data result to client browser via socket. I want that every users can see in real-time the latest result from their browser.
当服务器每秒运行PHP(通过cron作业)时,PHP脚本(abc123.php)将连接到数据库并通过套接字将结果返回给客户端浏览器。我希望每个用户都能实时查看浏览器的最新结果。
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var cronjob = require('cron').CronJob;
var runner = require("child_process");
io.on('connection', function (socket) {
var myJob = new cronjob('* * * * * *', function() {
var phpScriptPath = "/home/domain/public_html/abc123.php";
runner.exec("php " + phpScriptPath, function(err, phpResponse, stderr) {
socket.send(phpResponse);
});
}, null, true, 'Asia/Kuala_Lumpur');
myJob.start();
});
1 个解决方案
#1
0
The problem with your code is that you start php every second for every user. This will quite fast take a lot of CPU. You should run the PHP once, and send the result to all clients.
您的代码的问题是您每秒都为每个用户启动php。这将很快占用大量的CPU。您应该运行PHP一次,并将结果发送给所有客户端。
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var cronjob = require('cron').CronJob;
var runner = require("child_process");
var myJob = new cronjob('* * * * * *', function() {
var phpScriptPath = "/home/domain/public_html/abc123.php";
runner.exec("php " + phpScriptPath, function(err, phpResponse, stderr) {
io.send(phpResponse);
});
}, null, true, 'Asia/Kuala_Lumpur');
myJob.start();
#1
0
The problem with your code is that you start php every second for every user. This will quite fast take a lot of CPU. You should run the PHP once, and send the result to all clients.
您的代码的问题是您每秒都为每个用户启动php。这将很快占用大量的CPU。您应该运行PHP一次,并将结果发送给所有客户端。
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var cronjob = require('cron').CronJob;
var runner = require("child_process");
var myJob = new cronjob('* * * * * *', function() {
var phpScriptPath = "/home/domain/public_html/abc123.php";
runner.exec("php " + phpScriptPath, function(err, phpResponse, stderr) {
io.send(phpResponse);
});
}, null, true, 'Asia/Kuala_Lumpur');
myJob.start();