proc.php 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
  3. go(function () {
  4. $descriptorspec = array(
  5. 0 => array("pipe", "r"), // 标准输入,子进程从此管道中读取数据
  6. 1 => array("pipe", "w"), // 标准输出,子进程向此管道中写入数据
  7. 2 => array("file", __DIR__ . "/error-output.txt", "a") // 标准错误,写入到一个文件
  8. );
  9. $cwd = '/tmp';
  10. $env = array('some_option' => 'aeiou');
  11. $process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);
  12. // var_dump($process, $pipes);exit;
  13. // $pipes 现在看起来是这样的:
  14. // 0 => 可以向子进程标准输入写入的句柄
  15. // 1 => 可以从子进程标准输出读取的句柄
  16. // 错误输出将被追加到文件 /tmp/error-output.txt
  17. fwrite($pipes[0], '<?php sleep(1);print_r($_ENV); ?>');
  18. fclose($pipes[0]);
  19. // echo stream_get_contents($pipes[1]);
  20. // fclose($pipes[1]);
  21. // 切记:在调用 proc_close 之前关闭所有的管道以避免死锁。
  22. $return_value = proc_close($process);
  23. echo "command returned $return_value\n";
  24. });