amphp / process

An async process dispatcher for Amp.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

amphp/process kills sub process on exit

ostrolucky opened this issue · comments

wrapper.php

<?php
echo 'foo';
sleep(1);
touch('a');

Following works as expected, outputs foo, exits immediately and after 1 second creates file 'a'.

<?php

$fd = [
    ["pipe", "r"], // stdin
    ["pipe", "w"], // stdout
];
$handle = @\proc_open('php wrapper.php', $fd, $pipes);

echo fread($pipes[1], 100);

Following does not work as expected. Outputs foo, exits immediately but file is not created.

<?php
require_once "vendor/autoload.php";
\Amp\Loop::run(function () {
    $process = new \Amp\Process\Process("php wrapper.php");
    yield $process->start();
    echo yield $process->getStdout()->read();
});

If the Process object is destroyed while the process is running, the process is killed. If you want to wait for the process to finish execution, you need to wait on the promise returned from Process::join().

I don't want to wait, I want to exit immediately and let process finish in background

Currently we don't directly support daemonizing child processes. Maybe you can get it work with nohup / setsid? I tried, but I failed.

Yeah I don't know how to do that either, what you currently use at

'{ (%s) <&3 3<&- 3>/dev/null & } 3<&0; trap "" INT TERM QUIT HUP;' .
'pid=$!; echo $pid >&3; wait $pid; RC=$?; echo $RC >&3; exit $RC',
is black magic for me.

Also please remove wrong label.

A possible solution is adding a method to Process, perhaps daemonize(), that sets a flag to not automatically kill the child process when the Process object is destroyed. The current default behavior would not be altered, but that would enable what @ostrolucky is trying to accomplish.

yeah that would work for me

This won't be supported for now, because the process would have to be properly disowned to avoid zombie processes.