react-php/react — pull request #61 by yuya-takeyama (2012-08-18 11:58:38+00:00)
Title: WIP: ChildProcess

This is just a proposal.
And no unit-test code is available now.

Please give me advices for...

* Better API
* Should the constructor param of `Process` be `Stream` object or `resource` variable?
* Better naming
* `Factory`? `Executor`?
* Better implementation
* ..and so on

Below is the result of example code

```
$ php examples/child-process.php
[A] [PID] pid is 68466
[A] [CMD] php '-r' 'foreach (range(1, 3) as $i) { echo $i, PHP_EOL; sleep(1); } fputs(STDERR, "Bye.");'
[B] [PID] pid is 68467
[B] [CMD] php '-r' 'foreach (range(1, 6) as $i) { echo $i, PHP_EOL; sleep(1); } fputs(STDERR, "Bye.");'
[C] [PID] pid is 68468
[C] [CMD] php '-r' 'foreach (range(1, 9) as $i) { echo $i, PHP_EOL; sleep(1); } fputs(STDERR, "Bye.");'
[C] [STDOUT] string(2) "1\n"
[A] [STDOUT] string(2) "1\n"
[B] [STDOUT] string(2) "1\n"
[A] [STDOUT] string(2) "2\n"
[B] [STDOUT] string(2) "2\n"
[C] [STDOUT] string(2) "2\n"
[A] [STDOUT] string(2) "3\n"
[C] [STDOUT] string(2) "3\n"
[B] [STDOUT] string(2) "3\n"
[B] [STDOUT] string(2) "4\n"
[C] [STDOUT] string(2) "4\n"
[A] [STDERR] string(4) "Bye."
[A] [STDOUT] string(0) ""
[A] [STDERR] string(0) ""
[A] [EXIT] exited with status code 0
[B] [STDOUT] string(2) "5\n"
[C] [STDOUT] string(2) "5\n"
[B] [STDOUT] string(2) "6\n"
[C] [STDOUT] string(2) "6\n"
[B] [STDOUT] string(0) ""
[B] [STDERR] string(4) "Bye."
[B] [STDERR] string(0) ""
[B] [EXIT] exited with status code 0
[C] [STDOUT] string(2) "7\n"
[C] [STDOUT] string(2) "8\n"
[C] [STDOUT] string(2) "9\n"
[C] [STDERR] string(4) "Bye."
[C] [STDOUT] string(0) ""
[C] [STDERR] string(0) ""
[C] [EXIT] exited with status code 0
```

Currently, empty string is emitted when a process is exiting.
It seems because of implementation difference of `Stream->handleData()` and `Connection->handleData()`.

https://github.com/react-php/react/blob/2a43233661bd148c3eaa9214a89558669d202828/src/React/Socket/Connection.php#L14
https://github.com/react-php/react/blob/2a43233661bd148c3eaa9214a89558669d202828/src/React/Stream/Stream.php#L114

`Connection->handleData()` closes when hendled empty string and not emits `data` event then.
But `Stream->handleData()` emits empty string.

What is the difference for?