gpt4 book ai didi

javascript - 获取 WebAssembly instantiateStreaming 进度

转载 作者:行者123 更新时间:2023-12-03 08:29:46 24 4
gpt4 key购买 nike

WebAssembly.instantiateStreamingfastest下载并实例化 .wasm 模块的方法,但是对于大型 .wasm 文件,它仍然需要很长时间。在这种情况下,仅显示微调器并不能提供足够的用户反馈。

有没有办法使用 WebAssembly.instantiateStreaming api 并获取某种形式的进度事件,以便可以向用户显示 eta?理想情况下,我希望能够显示百分比进度条/估计剩余时间指示器,以便用户知道他们需要等待多长时间。

最佳答案

构建答案 here .

获取WebAssembly.instantiateStreaming的进度/WebAssembly.compileStreaming使用实现其自己的 Controller 的自定义 ReadableStream 创建新的 Fetch Response。

示例:

// Get your normal fetch response
var response = await fetch('https://www.example.com/example.wasm');

// Note - If you are compressing your .wasm file the Content-Length will be incorrect
// One workaround is to use a custom http header to manually specify the uncompressed size
var contentLength = response.headers.get('Content-Length');

var total = parseInt(contentLength, 10);
var loaded = 0;

function progressHandler(bytesLoaded, totalBytes)
{
// Do what you want with this info...
}

var res = new Response(new ReadableStream({
async start(controller) {
var reader = response.body.getReader();
for (;;) {
var {done, value} = await reader.read();

if (done)
{
progressHandler(total, total)
break
}

loaded += value.byteLength;
progressHandler(loaded, total)
controller.enqueue(value);
}
controller.close();
},
}, {
"status" : response.status,
"statusText" : response.statusText
}));

// Make sure to copy the headers!
// Wasm is very picky with it's headers and it will fail to compile if they are not
// specified correctly.
for (var pair of response.headers.entries()) {
res.headers.set(pair[0], pair[1]);
}

// The response (res) can now be passed to any of the streaming methods as normal
var promise = WebAssembly.instantiateStreaming(res)

关于javascript - 获取 WebAssembly instantiateStreaming 进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65491241/

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