0

我正在学习如何使用 Puppeteer 集群,我有一个问题。

如何使用按键中断在无限循环中运行的 puppeteer 集群执行?

代码将是这样的:

const { Cluster } = require('puppeteer-cluster');

const fs = require('fs').promises;
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function run() {
    const cluster = await Cluster.launch({
        concurrency: Cluster.CONCURRENCY_CONTEXT,
        maxConcurrency: 2,
        monitor: true,
    });

    await cluster.task(async ({ page, data: acc }) => {
        // Do task ~2 minutes
    });

    // In case of problems, log them
    cluster.on('taskerror', (err, data) => {
        console.log(`  Error crawling ${data}: ${err.message}`);
    });

    // Read the accs.csv file from the current directory
    const csvFile = await fs.readFile(__dirname + '/accs.csv', 'utf8');
    const lines = csvFile.split('\n');
    while(true){
        //for each account in the file
        for (let i = 0; i < lines.length; i++) {
            const line = lines[i];
            cluster.queue(line);
        }
        
        // sleep for a moment...
        await sleep(60000);
    }
    

    await cluster.idle();
    await cluster.close();
};

try{
    run();
} catch(e) {
    console.log(e.message());
}
4

1 回答 1

0

我设法像往常一样使用 readline 做到这一点。我认为它不起作用,因为终端上显示了监视器。

如果有人需要有关如何完成的示例,请检查以下内容:Break infinite loop user input nodejs

于 2021-08-17T03:06:19.720 回答