如何通过proc_open后台处理进程并可以访问STDIN? [英] How to background a process via proc_open and have access to STDIN?
问题描述
我很高兴使用 proc_open
将数据传递到另一个PHP进程中.像这样的东西
I'm happily using proc_open
to pipe data into another PHP process.
something like this
$spec = array (
0 => array('pipe', 'r'),
// I don't need output pipes
);
$cmd = 'php -f another.php >out.log 2>err.log';
$process = proc_open( $cmd, $spec, $pipes );
fwrite( $pipes[0], 'hello world');
fclose( $pipes[0] );
proc_close($process);
在另一个PHP文件中,我用以下命令回显STDIN:
In the other PHP file I echo STDIN with:
echo file_get_contents('php://stdin');
这很好用,但当我把它作为背景时就不行了.只需在 $ cmd
后面加上&
,我从STDIN中什么也得不到.我一定缺少基本的东西.
This works fine, but not when I background it. Simply by appending $cmd
with &
I get nothing from STDIN. I must be missing something fundamental.
它也失败,并显示 fgets(STDIN)
请问有什么想法吗?
推荐答案
您不能写入后台进程的STDIN(至少不是以通常的方式).
You can't write to STDIN of a background process (at least, not in the normal way).
有关服务器故障的问题可能会让您了解如何解决此问题.
This question on Server Fault may give you some idea of how to work around this problem.
不相关:您说不需要规范中的输出,但可以在 $ cmd
中指定它们;您可以这样编写 $ spec
:
Unrelated: you say do don't need outputs in the spec, yet you specify them im your $cmd
; you can write $spec
like this:
$spec = array (
0 => array('pipe', 'r'),
1 => array('file', 'out.log', 'w'), // or 'a' to append
2 => array('file', 'err.log', 'w'),
);
这篇关于如何通过proc_open后台处理进程并可以访问STDIN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!