我使用phantomJS打印PDF,带有phantomjs-node模块。它工作得很好,但当我试图一次创建几个文件时,它抛出一个未处理的错误"Listen EADDRINUSE。
我认为这是因为模块使用了phantomJS,这是一个外部进程,它不能多次将它绑定到同一个端口?
无论如何,我不能捕捉到这个错误,我想解决这个问题,至少在发生这种情况时避免服务器崩溃。我考虑使用一个“全局”变量,比如一个锁,以便在当前调用完成之前阻塞并发调用。
有没有关于如何实现它的想法,或者任何其他解决方案?
发布于 2013-08-03 07:29:19
来自@AndyD的代码不正确,imho。参见中的第45 - 54行
https://github.com/sgentle/phantomjs-node/blob/master/phantom.coffee
所以这个例子应该是
var portscanner = require('portscanner');
var phantom = require('phantom');
portscanner.findAPortNotInUse(40000, 60000, 'localhost', function(err, freeport) {
phantom.create({'port': freeport}, function(ph){
...
}
});发布于 2013-06-14 06:02:37
您应该能够在每次调用create时传入一个端口号:
var phantom = require('phantom');
phantom.create(null, null, function(ph){
}, null, 11111);然后,您可以使用计数器来确保每次启动phantomjs-node时都是不同的。
如果您每次都要启动一个新进程,并且不能共享计数器,那么您可以使用portscanner来查找空闲端口:
var portscanner = require('portscanner');
var phantom = require('phantom');
portscanner.findAPortNotInUse(40000, 60000, 'localhost', function(err, freeport) {
phantom.create(null, null, function(ph){
...
}
}, null, freeport);https://stackoverflow.com/questions/17089817
复制相似问题