gpt4 book ai didi

Javascript 循环、异步函数和 headless 浏览器

转载 作者:行者123 更新时间:2023-12-01 15:55:39 26 4
gpt4 key购买 nike

通过使用微软新的 headless 浏览器 playwright,我构建了一些既不返回错误也不返回其他内容的东西。

此时,我的想法已经结束了,我请你给我一些提示,指出我的失败。

这段代码应该只是启动一个多头 headless 浏览器组异步。但是浏览器的启动挂起并且应用程序处于无限循环中。我将代码粘贴到此处,它是一个简单的 nodejs 脚本,用于重现该行为。

感谢帮助和阅读 ;)

const playwright = require('playwright');

log('start playwright async');

let maxRunners = 1;
let running = 0;
let list = [1,2,3,4,5,6,7,8,9,0,11,12,13,14,15];

log('start job');

while (list.length > 0) {

if (running < maxRunners) {
log('runner started');
running++;

let entry = list[0];
list.shift();

log('start browser loop');
for (const browserType of ['chromium', 'firefox', 'webkit']) {
log('fire async');
(async () => {
log('loop next');
log('launch: ', browserType);
const browser = await playwright[browserType].launch({
headless: false
});
log(browserType, ' launched');
const context = await browser.newContext();
log('open new page');
const page = await context.newPage('http://whatsmyuseragent.org/');
log('page opened');
log('make screenshot');
await page.screenshot({path: `example-${browserType}.png`});
log('screenshot made');
log('close browser');
await browser.close();
log('browser closed');
log('loop succeed');

running--;
})();
log('end async');
}
log('end loop');

if (running === 0 && list.length === 0) {
log('job finished');
}
}
}

log('end playwright script');

function log(...msgs) {
let date = new Date();
let timeString = date.toISOString().substr(11, 8);
let msg = '';
for (let i in msgs) {
msg += msgs[i];
}

console.log(timeString, ':', msg);
}

输出:

20:53:29 : start playwright async
20:53:29 : start job
20:53:29 : runner started
20:53:29 : start browser loop
20:53:29 : fire async
20:53:29 : loop next
20:53:29 : launch: chromium
20:53:29 : end async
20:53:29 : fire async
20:53:29 : loop next
20:53:29 : launch: firefox
20:53:29 : end async
20:53:29 : fire async
20:53:29 : loop next
20:53:29 : launch: webkit
20:53:29 : end async
20:53:29 : end loop

最佳答案

您可以在代码中改进一些地方:

(async()=>{

log('start playwright async');

let maxRunners = 1;
let running = 0;
let list = [1,2,3,4,5,6,7,8,9,0,11,12,13,14,15];

log('start job');
const promises = [];
while (list.length > 0) {

if (running < maxRunners) {
log('runner started');
running++;

let entry = list[0];
list.shift();

log('start browser loop');
for (const browserType of ['chromium', 'firefox', 'webkit']) {
log('fire async');
promises.push((async () => {
log('loop next');
log('launch: ', browserType);
const browser = await playwright[browserType].launch({
headless: false
});
log(browserType, ' launched');
const context = await browser.newContext();
log('open new page');
const page = await context.newPage('http://whatsmyuseragent.org/');
log('page opened');
log('make screenshot');
await page.screenshot({path: `example-${browserType}.png`});
log('screenshot made');
log('close browser');
await browser.close();
log('browser closed');
log('loop succeed');

running--;
})());
log('end async');
}
log('end loop');
} else {
await Promise.all(promises);
}
}

await Promise.all(promises);
log('job finished');
log('end playwright script');

function log(...msgs) {
let date = new Date();
let timeString = date.toISOString().substr(11, 8);
//date.setSeconds(45); // specify value for SECONDS here
//var timeString = date.toISOString().substr(11, 8);
let msg = '';
for (let i in msgs) {
msg += msgs[i];
}

console.log(timeString, ':', msg);
}
})()

让我们将所有内容包装在一个异步函数中

(async()=>{
)();

然后,让我们跟踪这些任务/ promise :

const promises = [];
...
log('fire async');
promises.push((async () => {
})());

如果你离开了 worker ,你需要等待他们:

if (running < maxRunners) {
...
} else {
await Promise.all(promises);
}

你应该开始使用它。

关于Javascript 循环、异步函数和 headless 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60047202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com