PHP stream_select 不起作用

标签 php windows proc-open

我使用的是 Windows 7 和 php 5。

我在通过 proc_open 执行进程并检查超时时遇到问题。 我使用 stream_select 通过以下代码检查超时:

<?php
$descriptorspec = array(
0 => array("file", $infile, "r"),  // stdin is a pipe that the child will read from
1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a file to write to
);
$prog = @proc_open ($objname.".exe", $descriptorspec, $pipes, "$DOCUMENT_ROOT/judge/temp", null, null);
if(!is_resource($prog)) ErrorResult("","proc_open err");

$streams = array($pipes[1]);
$start = microtime(true);
$ret = stream_select($streams, $a = null,$a = null, 1);
$stop = microtime(true);
?>

这是我用来测试的 C++ 代码:

#include<windows.h>

int main(){
    Sleep(2000);
    return 0;
}

在该代码中,根本没有输出,但 stream_select 不会等待 1 秒并返回 1。

我该如何解决这个问题?

最佳答案

我怀疑您的 C++ 可执行文件可能由于某种原因并没有真正执行,但是 cmd.exe 可能对您隐藏了退出代码。

  • 获得 C++ 可执行文件实际运行的证明。在调用 stream_select 之前尝试将一些数据从 C++ 传递到 PHP。

  • cmd.exe 别挡路:

    $opts = array('suppress_errors' => false, 'bypass_shell' => true);

    $prog = proc_open ($objname.".exe", $descriptorspec, $pipes, "$DOCUMENT_ROOT/judge/temp", null, null, $opts);

关于PHP stream_select 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11329533/

相关文章:

php - Laravel 5 配置 - 环境和覆盖

javascript - 单击按钮时如何删除只读属性

windows - 排序对象对 Get-EventLog 没有影响

php - 向由 proc_open() 打开的进程发送信号

php - 对使用 proc_open 打开的进程进行多次写入

PHP 不从查询创建表

php - 收到数据后关闭 HTTP 连接

windows - git 如何在 Windows 上检测修改过的文件?

c# - 启动 C# Windows 应用程序并等待在命令行中退出

javascript - 如何修复 Process 类依赖于 proc_open,该类在您的 PHP 安装中不可用