Skip to content

Instantly share code, notes, and snippets.

@clairvy
Created June 15, 2010 15:37
Show Gist options
  • Select an option

  • Save clairvy/439270 to your computer and use it in GitHub Desktop.

Select an option

Save clairvy/439270 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
$stdout = fopen('php://stdout', 'w');
$stderr = fopen('php://stderr', 'w');
fwrite($stdout, "Hello stdout\n");
fwrite($stderr, "Hello stderr\n");
sleep(100);
#!/usr/bin/env php
<?php
function f() {
$descriptorspec = array(
array('pipe', 'r'),
array('pipe', 'w'),
array('pipe', 'w'),
);
$process = proc_open('./out.php', $descriptorspec, $pipes);
$ret = pcntl_waitpid(proc_get_pid($process), $status, WNOHANG);
echo "ret = $ret\n";
echo "close\n";
//proc_close($process);
$ret = posix_kill(proc_get_pid($process), SIGTERM);
echo "ret = $ret\n";
echo "wait\n";
while (pcntl_waitpid(proc_get_pid($process), $status) <= 0) {
}
$ret = proc_close($process);
echo "ret = $ret\n";
}
function proc_get_pid($process) {
$status = proc_get_status($process);
return $status['pid'];
}
f();
@clairvy
Copy link
Author

clairvy commented Jun 15, 2010

考えながら書いてるのでコードは汚い.

  • f() は,スコープを作るためにある.
    • $process のデストラクタ呼び出しのテストしたときの名残.
  • 普通に終了を待つなら,proc_close() でいいんだけど,
    • サーバを起動して待つタイプだと,posix_kill() を使う.
      • んだけど,PID を指定するのがメンドイ.
  • まぁ,$process は抽象化されてて楽な気はする.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment