在php中连接进程管道

编程入门 行业动态 更新时间:2024-10-28 09:19:52
本文介绍了在php中连接进程管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望将使用proc_open创建的一个进程的输出通过管道传递给使用proc_open创建的另一个进程(在php中).例如.在bash中,我可以执行以下操作:

I would like the output of one process created with proc_open to be piped to another one created with proc_open (in php). For example. In bash I can do:

[herbert@thdev1 ~]$ cat foo 2 3 1 [herbert@thdev1 ~]$ cat foo | sort 1 2 3 [herbert@thdev1 ~]$

我想使用proc_open(而不是shell_exec)在php中对此进行模拟,以便控制返回码,管道等.所以我想要这样的东西:

I would like to simulate this in php using proc_open (instead of shell_exec) in order to have control over return-codes, pipes, etc. So I want something like this:

$catPipes=array(); $sortPipes=array(); $cwd = '/tmp'; $env = array(); $catProcess = proc_open("cat foo", array( 0 => array("pipe", "r"), 1 => array("pipe", "w") ), $catPipes, $cwd, $env); $sortProcess = proc_open("sort", array( 0 => array("pipe", "r", $catPipes[1]), 1 => array("pipe", "w"), ), $sortPipes, $cwd, $env); echo stream_get_contents($sortPipes[1]); fclose($sortPipes[1]); //proc_close(this) ... proc_close(that) ... etc

有人会知道我如何模拟"|" php中bash的功能,即将cat-process的第二个描述符连接到sort-process的第一个描述符?任何帮助,将不胜感激!但是请不要将我重定向到shell_exec,因为我希望能够检查退出代码并记录错误:).

Would someone know how I can simulate the "|" of bash in php, i.e. connect the second descriptor of the cat-process to the first descriptor of the sort-process? Any help would be appreciated! But please do not redirect me to shell_exec, as I want to be able to check exit-codes and log errors :).

我的上班需要业务解决方案是:

My needs-to-work-business-solution btw is:

while(!feof($searchPipes[1])) fwrite($lookupPipes[0], stream_get_line($searchPipes[1], 40000));

这基本上是操作系统要执行的操作,但是我不想使用自己的管道管理,因为我为此使用了一个内核/posix,说实话,这不是1976:)

Which is basically what the OS would do, but I do not want to my own pipe-management, as I have a kernel/posix for that, and let's be honest, it's not 1976 :)

推荐答案

是的,您可以-但我认为您必须对此进行全面定义.您可以将"sort"的STDIN用作"cat"的STDOUT管道.看看以下对我有用的东西:

Yes, you can -- but i think you have to define this the way round. That you can use the STDIN of "sort" as STDOUT pipe for "cat". Have a look at the following, which works for me:

<?php $txt = "a\nc\ne\nb\nd\n"; $fh = fopen('data://text/plain;base64,' . base64_encode($txt), 'r'); $sort_pipes = array(); $sort_proc = proc_open( 'sort', array( array('pipe', 'r'), STDOUT ), $sort_pipes ); $cat_pipes = array(); $cat_proc = proc_open( 'cat', array( $fh, $sort_pipes[0] ), $cat_pipes );

在前两行中,我从文本字符串定义了一个数据流,而我不必依赖于文件系统中某个位置的文件.请注意,我在数据流(a,c,e,b,d)中存储了未排序字符的列表.运行上面的脚本应将排序后的列表返回到STDOUT.

In the first two rows i defined a data stream from a text string that i do not have to rely on a file somewhere in the filesystem. Note, that i have a list of unsorted characters stored in the data stream (a, c, e, b, d). Running the script above should return a sorted list to STDOUT.

请注意,您也可以将资源指定为描述符.在这种情况下,您必须省略数组符号,所以:

Note, that you can specify resources as descriptors, too. In this case you must omit the array notation, so:

STDOUT

代替

array(STDOUT)

顺便说一句:您甚至可以直接写入由文件名指定的文件.您可以在proc_open的手册条目中找到有关描述符规范的更多信息,网址为 http ://en.php/manual/de/function.proc-open.php

Btw.: you can even write directly to a file specified by a file name. You can find further information on the descriptor specification in the manual entry for proc_open at en.php/manual/de/function.proc-open.php

编辑

当然,另一种方法也起作用:您还可以将"cat"写入STDOUT管道array('pipe', 'w')并将$cat_pipes[1]用作STDIN的"sort". :)

The other way works too, of course: you can also write "cat" to an STDOUT pipe array('pipe', 'w') and use $cat_pipes[1] as STDIN for "sort". :)

更多推荐

在php中连接进程管道

本文发布于:2023-10-04 20:47:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管道   进程   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!