gpt4 book ai didi

node.js - 从 Node.js 中的管道传输大数据时的截断

转载 作者:太空宇宙 更新时间:2023-11-03 22:00:32 25 4
gpt4 key购买 nike

我正在尝试使用 Node 通过 HTTP 从子进程传输任意大量数据。我的完整代码是 here ,最重要的是:

  res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': 'attachment;filename=osm_export_' +
((north + south) / 2) + '_' + ((east + west) / 2) + '.pbf'
});

// run vex
var proc = spawn(cmd, [dbname, south, west, north, east, '-']);

// stream chunks
proc.stdout.pipe(res);

大约 40 MB(40,000,000 到 42,000,000 字节之间的任意位置)后,流会中断并且请求永远不会完成。我无法设置 Content-Length header ,因为我不知道该命令在完成之前会生成多少数据,我想知道这是否是缓冲区不足;有问题的命令是从数据库中提取数据并写入流,这可能比我的计算机和服务器之间的连接慢。我怀疑这是因为我用以下代码替换了代码:

var http = require('http');
var spawn = require('child_process').spawn;

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'application/octet-stream'});

var proc = spawn('head', ['-c', '500000000', '/dev/zero']);

proc.stdout.pipe(res);
}).listen(8181, '127.0.0.1');

它传输了 500MB 的空数据,并且运行良好。我需要设置某种超时等吗?

最佳答案

啊,发现问题了。这对其他人可能有用也可能没用。我没有对进程 stderr 流执行任何操作,后端进程将大量状态信息写入 stderr。 Node 内某处的缓冲区被填满,然后崩溃了。将流程创建行更改为

  var proc = spawn(cmd, [dbname, south, west, north, east, '-'], {stdio: ['ignore', 'pipe', 'ignore']});

解决了问题。感谢所有提供帮助的人!

关于node.js - 从 Node.js 中的管道传输大数据时的截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26868698/

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