cod*_*mer 5 javascript cluster-computing node.js express
我有一个 express.js 应用程序,它必须在每次有特定请求时运行一个子进程(这里是: /compute/real-time )。将有用户创建的脚本来计算数据。所以,我使用节点集群模块来创建一个工作池并选择一个可以自由执行脚本的工作。但是我在创建集群本身的过程中遇到了麻烦。这是代码 clusterPool.js
var cluster = require('cluster');
exports.setupCluster = function(){
console.log ("Setting up cluster for bot processing " )
if (cluster.isMaster){
cluster.fork(); //one is good enough at the moment
}
}
Run Code Online (Sandbox Code Playgroud)
计算.js
var async = require('async');
var clusterPool = require('.././clusterPool')
//Start the cluster
clusterPool.setupCluster();
exports.computeRealTime = function(req,res){
clusterPool.cluster.on("listening",function(worker){
//....Do something with the worker
})
}
Run Code Online (Sandbox Code Playgroud)
网络服务器.js
// Include Express
var express = require('express');
var compute = require('.././compute');
// Create a new Express application
var app = express();
// Add a basic route – index page
app.get('/compute/real-time',compute.computeRealTime);
// Bind to a port
app.listen(3000);
Run Code Online (Sandbox Code Playgroud)
这是我面临的错误:
error: code=EADDRINUSE, errno=EADDRINUSE, syscall=bind
error: Error: bind EADDRINUSE
at errnoException (net.js:884:11)
at net.js:1056:26
at Object.1:2 (cluster.js:587:5)
at handleResponse (cluster.js:171:41)
at respond (cluster.js:192:5)
at handleMessage (cluster.js:202:5)
at process.EventEmitter.emit (events.js:117:20)
at handleMessage (child_process.js:318:10)
at child_process.js:392:7
at process.handleConversion.net.Native.got (child_process.js:91:7)
Run Code Online (Sandbox Code Playgroud)
请问这个问题有什么出路吗?提前致谢
这是其中一种情况,当您的服务器代码遇到错误并且由于错误处理不当,我们会收到异常,但端口仍然繁忙。因此,您需要清除保留该端口的应用程序。如果您使用的是linux,则可以使用
lsof -i :3000
Run Code Online (Sandbox Code Playgroud)
获取进程 ID 并使用以下命令终止该进程
kill -9 #processId
Run Code Online (Sandbox Code Playgroud)
然后重新启动您的服务器。
| 归档时间: |
|
| 查看次数: |
1850 次 |
| 最近记录: |