N
N
Nikolay372021-05-16 01:16:24
Node.js
Nikolay37, 2021-05-16 01:16:24

How to run multiple threads with Puppeter in node js?

First - I need to open several windows with Puppeter, for this - I call the function through a loop, with different parameters. After that - the function sends a couple of requests and after - calls this function with Puppeter

function startPup(){
    (async () => {
      const browser = await puppeteer.launch()
      const page = await browser.newPage();
      await page.goto('https://...');
      await page.addScriptTag({path: 'script.js'});
      try{
        await page.waitForSelector('#name')
        html = await page.content()
        console.log(html )
        await browser.close();
      }
      catch{
        console.log(html)
        await browser.close();
      }
    })()
}

But, the bottom line is that this is all done synchronously, including the call to Puppeters. And they are apparently related to each other and as a result - the puppeter who will quickly receive the selector with id = name will execute the script and close the page. The second one, apparently, having received the same data from the script, will also close. I need - so that they are not connected to each other and are launched in an asynchronous order

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
coderisimo, 2021-05-16
@coderisimo

QUEUES will help you :) . There are even dedicated solutions for Puppeter - https://github.com/thomasdondorf/puppeteer-cluster .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question