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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算