[英]Puppeteer Sharp: Multiple Browsers Concurrently
現在我正在使用 puppeteer(NodeJS 庫)將 HTML 轉換為 PDF 文檔。 雖然這是“工作”,但我正在移植到 Puppeteer-Sharp(C# 庫)。
我一切正常,但我有點擔心同時運行多個瀏覽器。
例如,我在同一台機器上的兩個不同進程中運行相同的代碼:
// Magic function. Downloads Chrome to a specific directory for the process.
var browser = GetBrowser();
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
var pdf = await page.PdfAsync();
我的問題:
這里有潛在的並發問題嗎?
我的(有限)理解是該庫使用 websockets 向 Chrome 發出指令,我不確定瀏覽器是否有可能相互“碰撞”。
本質上,我在問是否有可能接收到的 PDF 字節(通過await page.PdfAsync();
)將來自“其他”瀏覽器。
如果有什么安慰的話,瀏覽器是從每個進程的特定目錄下載和啟動的,所以從技術上講,它不是 Chrome 的“相同”實例被啟動兩次(但它確實是)。
您不需要多個瀏覽器,您可以使用帶有多個選項卡(或 Puppeteer 所稱的頁面)的一個瀏覽器。 這是我的示例代碼,它解決了您所做的相同事情(將 HTML 轉換為 PDF)。 它創建一個瀏覽器實例,該實例被傳遞給四個進程(可能更多),每個進程都創建和刪除自己的頁面。
public class PuppeteerSharpSample {
public async Task CreatePdfBatch(IEnumerable<string> urlList)
{
await using var browser = await Puppeteer.LaunchAsync( new LaunchOptions { Headless = true, ExecutablePath ="PathToChromeOrChromium.exe"};).ConfigureAwait(false);
await urlList.ForEachAsync(4, async url =>
{
await PrintPdf(url, browser).ConfigureAwait(false);
})
.ContinueWith(t =>
{
if (t.Exception != null)
{
throw t.Exception;
}
})
.ConfigureAwait(false);
}
private async Task PrintPdf(Browser browser, string Url)
{
await using var page = await browser.NewPageAsync().ConfigureAwait(false);
await page.GoToAsync(url).ConfigureAwait(false);
await page.PdfAsync("pdfFileNameMustBeMadeUniqueOfCourse.pdf").ConfigureAwait(false);
}
}
public static class HelperClass
{
//taken from https://scatteredcode.net/parallel-foreach-async-in-c/
public static Task ForEachAsync<T>(this IEnumerable<T> source, int dop, Func<T, Task> body)
{
async Task AwaitPartition(IEnumerator<T> partition)
{
using (partition)
{
while (partition.MoveNext())
{
await body(partition.Current).ContinueWith(t =>
{
if (t.IsFaulted && t.Exception != null)
{
throw t.Exception;
}
})
.ConfigureAwait(false);
}
}
}
return Task.WhenAll(
Partitioner
.Create(source)
.GetPartitions(dop)
.AsParallel()
.Select(AwaitPartition));
}
}
附帶說明:如果您的機器上安裝了 Chromium(可以是 Chrome、Chromium 或新的 Edge),您也不需要使用GetBrowser()
。 然后你可以只指向.exe,如上面的代碼所示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.