dongwei1855 2016-06-15 20:51
浏览 164

并行运行proc_open()命令 - PHP Linux

I'm attempting to run a process on a ubuntu server from PHP but it appears to hang up when you try it with multiple tabs open, although it seems to work on multiple devices. The code is:

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

ini_set('output_buffering', 'off');
ini_set('zlib.output_compression', false);
ini_set('implicit_flush', true);
ob_implicit_flush(true);

while (ob_get_level() > 0) 
{
  $level = ob_get_level();
  ob_end_clean();
  if (ob_get_level() == $level) break;
}

if (function_exists('apache_setenv')) 
{
  apache_setenv('no-gzip', '1');
  apache_setenv('dont-vary', '1');
}

$command = 'ping -c 5 8.8.8.8';

$cwd = '/tmp';
$descriptorspec = 
[
  0 => ['pipe', 'r'],
  1 => ['pipe', 'w'],
  2 => ['pipe', 'w']
];

$process = proc_open($command, $descriptorspec, $pipes, $cwd);
$proc_details = proc_get_status($process);
$pid = $proc_details['pid'];
$_SESSION['upid'] = $pid;

if (is_resource($process)) 
{
  while ($s = fgets($pipes[1])) 
  {
    print $s;
    flush();
  }
}

This produces the desired effect of running the ping command and outputting the result each line at a time but if I open up another tab and run it, the second tab won't run until the command in the first is finished.

How can I prevent the waiting and have both tabs run in parallel?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
    • ¥15 onvif+openssl,vs2022编译openssl64