I am looking for the best way to pipe data from one stream to another without having to deal with buffering myself. Basically the equivalent of the node.js stream.pipe function.

我正在寻找将数据从一个流传输到另一个流的最佳方法,而无需自己处理缓冲。基本上相当于node.js stream.pipe函数。

There is stream_copy_to_stream, but according to the comments on the manual page it is quite a memory hog (possibly reading the whole stream into memory?).

有stream_copy_to_stream,但根据手册页上的注释,这是一个非常大的内存(可能将整个流读入内存?)。

Of course I could do something like this:

当然我可以这样做:

$fh = fopen('filename.txt', 'rb');
$out = STDOUT;
while (!feof($fh)) {
    $bytes = 1024;
    fwrite(STDOUT, fread($fh, $bytes), $bytes);
}
fclose($fh);

But I'm hoping there is an easier API to accomplish this, without having to do the buffering myself.

但我希望有一个更简单的API来完成这个,而不必自己做缓冲。

Thanks.

1 个解决方案

#1


3

I downloaded the source code of PHP 5.4.0 and looked up stream_copy_to_stream (streams.c line 1419). It looks reasonable in my opinion.

我下载了PHP 5.4.0的源代码并查找了stream_copy_to_stream(streams.c第1419行)。在我看来,它看起来很合理。

First it tries to memory-map the source stream, and write the whole thing to the destination in one go without using an intermediate buffer.

首先,它尝试对源流进行内存映射,并在不使用中间缓冲区的情况下一次性将整个内容写入目标。

If memory mapping is not possible, then it uses an internal buffer (8192 bytes, see CHUNK_SIZE in php_streams_int.h line 49) to shuffle the data from the source to the destination.

如果无法进行内存映射,则它使用内部缓冲区(8192字节,请参阅php_streams_int.h第49行中的CHUNK_SIZE)将数据从源缓冲到目标。

Either the manual comment is wrong, or the developers have improved the function since it was first written. I would give it another look.

手动注释是错误的,或者开发人员自首次编写以来已经改进了该功能。我再试一次。

更多相关文章

  1. MySql-cluster中NDBD进程占用内存能否通过配置修改
  2. MySQL在C++中使用后务必释放 result,否则会造成内存泄露
  3. 基于缓冲区数据创建文件
  4. 我在Firefox中遇到内存泄漏或内存使用率高吗?
  5. AngularJS - 删除绑定以避免内存泄漏
  6. Python是如何进行内存管理的
  7. python之内存概念
  8. 用于Python项目的低内存和最快查询数据库
  9. 在Python中的大文件中拆分行的内存问题

随机推荐

  1. mysql中在表中insert数据时,有重复主键id
  2. mysql 树形结构查询(存储过程)
  3. 如何将sql查询中的值存储到变量中?
  4. 数据库_MySQL_复杂SQL的书写顺序与执行过
  5. mysql(workbench)更新数据时候的一个异常
  6. mysql dos命令 创建表单,选择数据库
  7. 什么是最好的Node.js mysql模块通过ssl连
  8. mysql utf8中文排序
  9. java系列-安装MySql(三)
  10. MySQL5.7以上版本root用户空密码修改(wind