FileReader 异步读取方法是否会阻塞主线程?
Does FileReader async read methods block the main thread?
我知道异步方法应该是非阻塞的。但我通常看到它们适用于外部操作,如 fetch()
。 即: 在浏览器之外处理的事情。
但是 FileReader()
API 呢?文件处理是浏览器完成的吧?
const reader = new FileReader();
reader.onload = (event) => {
console.log(event.target.result);
};
reader.onerror = (event) => {
console.log(event.target.result);
};
// **ONE** OF THE POSSIBLE METHODS BELOW
reader.readAsText(file);
reader.readAsArrayBuffer(file);
reader.readAsBinaryString(file);
reader.readAsDataURL(file);
问题
如果我读取一个 100Gb 的文件,它会在某个时候阻塞我的主线程吗?我的意思是,即使它在运行之前等待调用堆栈为空,它是否会在处理一些大文件时阻塞我的主线程?在这种情况下它是如何工作的?
无论答案是什么,它是否适用于运行最终由浏览器处理的异步操作的任何方法?
是 "async"。
数据HardDrive/memory访问等会并行完成,这通常需要更长的时间,为此浏览器不需要阻塞主线程,这是基本的I/O 手术。
实际reading and processing of the binary data to whatever format you asked has to be done in parallel.
To run steps in parallel means those steps are to be run, one after another, at the same time as other logic in the standard (e.g., at the same time as the event loop). This standard does not define the precise mechanism by which this is achieved, be it time-sharing cooperative multitasking, fibers, threads, processes, using different hyperthreads, cores, CPUs, machines, etc. By contrast, an operation that is to run immediately must interrupt the currently running task, run itself, and then resume the previously running task.
当然,我们不能确定它是真正的并行,因为硬件可能不支持并发,但从规格来看,它是异步的。
现在,读取 100GB 的文件肯定会抛出一个错误,指出您没有足够的可用内存。而且,如果您确实有足够的内存,那么您的计算机有可能无论如何都会遭受如此大的数据块的困扰。
同理,生成的数据通过.result属性传回你的线程会占用内存。处理太大的数据可能会影响页面的性能。
我知道异步方法应该是非阻塞的。但我通常看到它们适用于外部操作,如 fetch()
。 即: 在浏览器之外处理的事情。
但是 FileReader()
API 呢?文件处理是浏览器完成的吧?
const reader = new FileReader();
reader.onload = (event) => {
console.log(event.target.result);
};
reader.onerror = (event) => {
console.log(event.target.result);
};
// **ONE** OF THE POSSIBLE METHODS BELOW
reader.readAsText(file);
reader.readAsArrayBuffer(file);
reader.readAsBinaryString(file);
reader.readAsDataURL(file);
问题
如果我读取一个 100Gb 的文件,它会在某个时候阻塞我的主线程吗?我的意思是,即使它在运行之前等待调用堆栈为空,它是否会在处理一些大文件时阻塞我的主线程?在这种情况下它是如何工作的?
无论答案是什么,它是否适用于运行最终由浏览器处理的异步操作的任何方法?
是 "async"。
数据HardDrive/memory访问等会并行完成,这通常需要更长的时间,为此浏览器不需要阻塞主线程,这是基本的I/O 手术。
实际reading and processing of the binary data to whatever format you asked has to be done in parallel.
To run steps in parallel means those steps are to be run, one after another, at the same time as other logic in the standard (e.g., at the same time as the event loop). This standard does not define the precise mechanism by which this is achieved, be it time-sharing cooperative multitasking, fibers, threads, processes, using different hyperthreads, cores, CPUs, machines, etc. By contrast, an operation that is to run immediately must interrupt the currently running task, run itself, and then resume the previously running task.
当然,我们不能确定它是真正的并行,因为硬件可能不支持并发,但从规格来看,它是异步的。
现在,读取 100GB 的文件肯定会抛出一个错误,指出您没有足够的可用内存。而且,如果您确实有足够的内存,那么您的计算机有可能无论如何都会遭受如此大的数据块的困扰。
同理,生成的数据通过.result属性传回你的线程会占用内存。处理太大的数据可能会影响页面的性能。