所以我发现了puppeteer-cluster,它看起来像是一个非常有用的库,但是有一件我遇到的困难,如果能得到任何帮助,我将不胜感激。事情是所有示例,首先是在异步函数中调用Cluster.launch,然后是cluster.task等。我需要的是能够以一种可以在其他函数中使用它的方式声明Cluster实例。
User.findOne({pins: mongoose.Types.ObjectId(pin._id)})
.exec()
.then(async user => {
if (user) {
//The function being called should become cluster.execute() i think
let status = await publish_pin(user, pin);
因此,在多个数据库发现并循环之后,我上面调用了一个函数,该函数应该是集群中排队的那个函数。必须通过cluster.task执行的任务是publish_pin函数内部的逻辑。唯一的问题是我在哪里声明Cluster软件包的这一部分:
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_BROWSER,
monitor:true,
maxConcurrency: 2,
puppeteerOptions:{headless:false}
});
我不能把它放在那里,因为我不希望它在循环中创建,我只想要它在某个地方,之后我就可以在代码中的任何地方访问它,例如全局变量。我尝试过:
(async () => {
global.cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_BROWSER,
monitor:true,
maxConcurrency: 2,
puppeteerOptions:{headless:false}
});;
})();
就在顶部,但是在任何地方使用cluster
变量都会使我找不到簇错误。请帮忙。