amphp / process

An async process dispatcher for Amp.
MIT License
235 stars 30 forks source link

More pipes? #75

Open LacticWhale opened 1 month ago

LacticWhale commented 1 month ago

Is there a way to open more pipes? In normal PHP I can use proc_open to do something like this:

$descriptors = array(
    0 => array("pipe", "r"),  // STDIN
    1 => array("pipe", "w"),  // STDOUT
    2 => array("pipe", "w"),  // STDERR
    3 => array("pipe", "w"),  // DATA
);
$proc = proc_open("data_handler", $descriptors, $pipes, "../../../submodules/data_handler/program");
foreach ($inputs as $input) 
    fwrite($pipes[0], $input.PHP_EOL);
fclose($pipes[0]);

$data   = stream_get_contents($pipes[3]); fclose($pipes[3]);
$stderr = stream_get_contents($pipes[2]); fclose($pipes[2]);
$stdout = stream_get_contents($pipes[1]); fclose($pipes[1]);
$exitCode = proc_close($proc);

I couldn't find any documentation on this.

kelunik commented 1 month ago

Currently this is not supported. What are you trying to do?

LacticWhale commented 1 month ago

What are you trying to do?

I just had a program which used stdout for debug info and an additional pipe for data transfer. It's not a big problem I can move info to stderr and use stdout for data. But it would be nice to have support for this in the future.

kelunik commented 1 month ago

If you control both sides, you might find the IPC API of amphp/parallel helpful: https://github.com/amphp/parallel?tab=readme-ov-file#ipc

LacticWhale commented 1 month ago

I use it to run programs written in different languages (python, dart). I just did:

list($command, $id, $data) = explode(':', $chunk);

Instead of using different pipes. I guess I could have done this from begining.

But still I think it might be useful to get support for pipes in future.