简体   繁体   English

node.js - 如何调整 child_process.spawn() 的标准输出缓冲区大小(又名 highWaterMark)?

[英]node.js - How to adjust the the stdout buffer size (aka highWaterMark) of child_process.spawn()?

I'm attempting to adjust the buffer size (highWaterMark) for the stdout from a child_process.spawn() call, and I'm having a hard time doing this successfully.我正在尝试通过 child_process.spawn() 调用调整stdout的缓冲区大小(highWaterMark),但我很难成功地做到这一点。 Below is some code that illustrates what I'm looking for.下面是一些代码,说明了我在寻找什么。 I'm trying to perform some benchmarking with various stdout buffer sizes, but until I can actually adjust the buffer size, I won't be able to do so.我正在尝试使用各种标准输出缓冲区大小执行一些基准测试,但在我实际调整缓冲区大小之前,我将无法这样做。 Any advice would be appreciated, thanks!任何建议将不胜感激,谢谢!

// I'm using the `stdout` from a child_process.spawn() call to feed into another stream, and I'm having a hard time increasing the buffer size on stdout.
// The code below illustrates the issue:
const child_process = require('child_process');
const stream = require('stream');
const fs = require('fs');

const compressStream = child_process.spawn("cat", ["testfile_32MB"], {writableHighWaterMark: 1024*1024, highWaterMark: 1024*1024});  // <-- child_process.spawn does not have highWaterMark or writableHighWaterMark options, so including them has no effect, but I'm including it just as an illustration of what I want.
compressStream.stdout.writableLength = 1024*1024; // <-- This does not help.
compressStream.stdout._writableState.writableLength = 1024*1024; // <-- This does not help.
compressStream.stdout._writableState.writableHighWaterMark = 1024*1024; // <-- This does not help.
compressStream.stdout.writableHighWaterMark = 1024*1024; // <-- This does not help.
compressStream.stdout._readableState.highWaterMark = 1024*1024; // <-- This does not help.
compressStream.stdout._writableState.highWaterMark = 1024*1024; // <-- This does not help.
compressStream.stdout.on("data", (data) => {
  console.log("data length [from spawn()]:", data.length); // <-- data.length will always be <= 64KB! How can we get it higher?
});

const readStream = fs.createReadStream("testfile_32MB", {highWaterMark: 1024*1024});
readStream.on("data", (data) => {
  console.log("data length [from createReadStream()]:", data.length); // <-- This prints values <= 1MB, which is what I wish I had for child_process.spawn/stdout...
});

Note, you'll need a file called "testfile_32MB" in place to run this example code, and it should be at least a few megabytes in size.请注意,您需要一个名为“testfile_32MB”的文件来运行此示例代码,它的大小至少应为几兆字节。

Alas, you cannot do this.唉,你不能这样做。 highWaterMark is a mechanism that is used to signal the writer to stop because the reader is not reading and its buffer is full. highWaterMark是一种机制,用于指示写入器停止,因为读取器没有读取并且它的缓冲区已满。

A Socket will send you the data as soon as it is available.一旦数据可用, Socket就会向您发送数据。 It will always use an internal hard-coded 64k buffer.它将始终使用内部硬编码的 64k 缓冲区。 Increasing the highWaterMark will make it capable of holding more data, but it will still send you 64Kb chunks - except that you will get bursts of several calls per event loop rotation.增加highWaterMark将使其能够容纳更多数据,但它仍会向您发送 64Kb 块 - 除了您将在每个事件循环轮换时获得多个调用的突发。

You don't have access to the Stream creation options when using spawn - you can eventually pass a preconstructed named pipe stream - but this won't solve your problem.使用spawn时,您无权访问Stream创建选项 - 您最终可以传递一个名为 pipe stream 的预构建 - 但这不会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM