- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道异步方法应该是非阻塞的。但我通常看到它们应用于诸如 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 文件,是否会在某个时刻阻塞我的主线程?我的意思是,即使它在运行之前等待调用堆栈为空,每当它处理一些大文件时,这是否会阻塞我的主线程?在这种情况下它是如何工作的?
无论答案是什么,它是否适用于运行最终由浏览器处理的异步操作的任何方法?
最佳答案
是的,它是“异步”。
数据硬盘/内存访问等将并行完成,这通常需要更长的时间,为此浏览器不需要阻塞主线程,这是基本的 I/O 操作。
实际reading and processing of the binary data无论您要求什么格式都必须完成 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.
当然,我们不能确定它是真正的并行性,因为硬件可能不支持并发,但从规范的 Angular 来看,它是异步的。
现在,读取 100GB 文件肯定会抛出一个错误,指出您没有足够的可用内存。如果您确实有足够的内存,那么您的计算机很可能会因如此大的数据 block 而受到影响。
同样,生成的数据通过 .result 属性发送回线程时也会占用内存。处理太大的数据可能会影响页面的性能。
关于javascript - FileReader 异步读取方法是否会阻塞主线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56522431/
第一次在这里发帖。 我正在编写一个 Java 程序,它接受一个输入文本文件,读取内容,然后将它们打印到屏幕上,同时创建一个包含内容的输出文件。我已经设置了必要的编写器,但是当我尝试使用 Buffere
类 FileReader 中的构造函数 FileReader 不能应用于给定类型; import java.io.*; public class FileReader{ publ
任何人都可以帮我修复我的代码,以便我可以编译它。 当我编译它时,会出现一条错误消息“对‘FileReader::FileReader()’的 undefined reference ”。 #ifnde
这是我的程序。我必须从 JFileChooser 选择一个文件,然后对其执行一些操作。问题是,在我选择文件之前,filereader 给出了 nullpointer ,说实话,我什至无法选择,因为当
我无法真正理解 Java 中 FileReader 和 BufferedReader 类的用途。 在 docs.oracle 中,建议将缓冲读取器包装在 FileReader 对象周围,因为直接使用
我正在尝试读取 CSV 或 Excel 文件以将其转换为表格。现在我刚刚创建了一个类来读取输入文件: 输入是: 现在我刚刚创建了一个类来读取输入文件: class InputReader_tiqe
我正在玩弄食物识别 API。 我有一个名为 ingredients 的本地状态组件。 在组件中,我有一个 input 标签,它接受文件图像上传并调用 cameraHandler 方法 onChange
我正在尝试使用此应用脚本将文件上传到 Google 云端硬盘,它适用于较小的文件,但在上传大于约 1 GB 的文件时失败 + 一旦加载大文件,onerror 函数就会执行。我无法弄清楚如何在切片中读取
我的代码 - $selectFile = $(''); $selectFile.click(function () { this.value = null; });
我有一个 FileReader,可以让用户将文件(图像)上传到我的网站。 这是执行读取的代码: $("input[type='file']").change(function(e) { var
我知道异步方法应该是非阻塞的。但我通常看到它们应用于诸如 fetch() 之类的外部操作。 即:在浏览器外部处理的内容。 但是 FileReader() API 又如何呢?文件处理是由浏览器完成的,对
我有(可能)很容易解决的问题,但我自己无法解决。基本上我的reader.width和reader.height一直说未定义:/..其他一切都按预期工作 function handleFileSelec
我想像asp.net一样获取图像文件流并将它们传递到后台,但是每次我尝试触发onload事件时,它总是在编程通过后完成。 我尝试使用setTimeout来阻止它并让它处理并等待它成功,但它失败了。下面
我有两个类。首先是 TEXT 类:这里我读取了一个 6 行的文本文件。我只想逐行阅读,但这可行。但我想从第三行开始,并跳过最后一行,我只希望以 这是文本文件代码。 这是文件读取器的代码:
我仍在学习过程中,如果我有误解,请纠正我,但 FileReader 对象不应该返回文本文件的全部内容吗? 我这里有一段代码,我简单地尝试获取一个短.txt文件的内容,并使用system.out.pri
我收到错误“未处理的异常:java.io.FileNotFoundException,虽然我确定文件路径,但需要一些帮助 public static void main(String[] args)
我有一个我看不到的简单问题,希望另一双眼睛可以帮助我解决。除了我认为的文件阅读器之外,一切都运行良好。我应该让用户输入将其放入文件中,读取该文件并执行正确的操作。但由于某种原因,它只对用户输入的最后一
我尝试使用java.io.FileReader读取一些文本文件并将它们转换为字符串,但我发现结果编码错误并且根本不可读。 这是我的环境: Windows 2003,操作系统编码:CP1252 Java
在java中使用FileReader方法,使文件只读还是仍然需要声明 FileReader file = new FileReader(args[0]); BufferedReader buff
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭10 年前。 Improve th
我是一名优秀的程序员,十分优秀!