Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cluster execute not running in parallel #481

Closed
maxwill-max opened this issue Aug 28, 2022 · 1 comment
Closed

cluster execute not running in parallel #481

maxwill-max opened this issue Aug 28, 2022 · 1 comment

Comments

@maxwill-max
Copy link

/** cluster.execute */
const { Cluster }=require('puppeteer-cluster')
const vanillaPuppeteer = require('puppeteer')
const { addExtra } = require('puppeteer-extra')
const Stealth = require('puppeteer-extra-plugin-stealth')
const AdblockerPlugin = require('puppeteer-extra-plugin-adblocker')

async function main() {
    const puppeteer = addExtra(vanillaPuppeteer)
    puppeteer.use(Stealth())
    puppeteer.use(AdblockerPlugin())  
    
    const cluster = await Cluster.launch({
        puppeteer,
        maxConcurrency: 7,
        concurrency: Cluster.CONCURRENCY_BROWSER,
        puppeteerOptions:{headless:true,},
        monitor:true
    })    
    
    cluster.on('taskerror', (err, data) => {
        console.log(`Error crawling1 ${data}: ${err.message}`)
    })
    
    await cluster.task(async ({ page, data: url }) => {        
        await page.goto(url)
        const ip = await page.content()
        return ip
    })
    
    try {
        for (var i=0; i<100; i++){
        const ip=await cluster.execute('https://httpbin.org/ip')       
        }
    } catch (err) {
        console.log(err)
    }
    await cluster.idle()
    await cluster.close()
}
main().catch(console.warn)

1

max concurrency is to 7 and right now, 5 workers are started but at any time, only one 1 worker is actively running

script moves to next worker only after the current worker is finished.

I expected the script would run 7 workers and wait for any of the workers to finish to start the next so effectively, utilizing all 7 browser contexts
I expected the script would move to the next worker only if there is a available spot in concurrency

@thomasdondorf
Copy link
Owner

You are using cluster.execute which waits for the task to finish. You want to use cluster.queue instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants