大文件的事件循环? [英] Event loop for large files?

查看:27
本文介绍了大文件的事件循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我没记错的话,我记得异步 I/O(Node.js、Nginx)的事件循环"模型不太适合处理大文件.

If I'm not mistaken, I remember the "event loop" model of asynchronous I/O (Node.js, Nginx) not being well-suited for the sake of serving large files.

是这样吗,如果是,是否有解决方法?我正在考虑在 Node 中编写一个实时文件浏览器/文件服务器,但文件可能从 100MB 到 ~3GB 不等.我会假设事件循环会阻塞,直到文件完全提供?

Is this the case, and if so, are there methods around it? I'm considering writing a real-time file explorer / file server in Node, but files could be anywhere from 100MB to ~3GB. I would assume that the event loop would block until the file is completely served?

推荐答案

不会,不会被屏蔽.node.js 将分块读取文件,然后将这些块发送给客户端.在块之间,它将为其他请求提供服务.

No, it will not be blocked. node.js will read a file in chunks and then send those chunks to the client. In between chunks it will service other requests.

读取文件&通过网络发送数据是 I/O 绑定操作.node.js 将首先要求操作系统读取文件的一部分,当操作系统正在执行该操作时,node.js 将服务另一个请求.当操作系统将数据返回给 node.js 时,node.js 将告诉操作系统将该数据发送给客户端.在发送数据时,node.js 将为另一个请求提供服务.

Reading files & sending data over the network are I/O bound operations. node.js will first ask the operating system to read a part of a file and while the OS is doing that node.js will service another request. When the OS gets back to node.js with the data, node.js will then tell the OS to send that data to the client. While the data is being sent, node.js will service another request.

自己试试:

创建一个大文件

dd if=/dev/zero of=file.dat bs=1G count=1

运行这个 node.js 应用

Run this node.js app

var http = require('http');
var fs = require('fs');

var i = 1;
http.createServer(function (request, response) {
    console.log('starting #' + i++);
    var stream = fs.createReadStream('file.dat', { bufferSize: 64 * 1024 });
    stream.pipe(response);
}).listen(8000);

console.log('Server running at http://127.0.0.1:8000/');

多次请求 http://127.0.0.1:8000/ 并观察 node.js 处理它们.

Request http://127.0.0.1:8000/ several times and watch node.js handle them all.

如果您要提供大量大文件,您可能需要尝试不同的 bufferSize 值.

If you're going to serve lots of large files, you may want experiment with differnt bufferSize values.

这篇关于大文件的事件循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆