bug #18150 [Process] Wait a bit less on Windows (nicolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

[Process] Wait a bit less on Windows

| Q             | A
| ------------- | ---
| Branch        | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

By using `stream_select` instead of `usleep` when the input is at wait, and by not blocking when the process has halted, we can enhance a bit the performance on Windows.

Commits
-------

380a54f [Process] Wait a bit less on Windows
This commit is contained in:
Nicolas Grekas 2016-03-13 11:16:27 +01:00
commit 8feb12184c
3 changed files with 13 additions and 9 deletions

View File

@ -102,7 +102,7 @@ class UnixPipes extends AbstractPipes
unset($r[0]);
// let's have a look if something changed in streams
if ($r && false === $n = @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
if (($r || $w) && false === $n = @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
// if a system call has been interrupted, forget about it, let's try again
// otherwise, an error occurred, let's reset pipes
if (!$this->hasSystemCallBeenInterrupted()) {

View File

@ -106,11 +106,15 @@ class WindowsPipes extends AbstractPipes
public function readAndWrite($blocking, $close = false)
{
$this->unblock();
$this->write();
$w = $this->write();
$read = $r = $e = array();
$read = array();
if ($this->fileHandles && $blocking) {
usleep(Process::TIMEOUT_PRECISION * 1E6);
if ($blocking) {
if ($w) {
@stream_select($r, $w, $e, 0, Process::TIMEOUT_PRECISION * 1E6);
} elseif ($this->fileHandles) {
usleep(Process::TIMEOUT_PRECISION * 1E6);
}
}
foreach ($this->fileHandles as $type => $fileHandle) {
$data = stream_get_contents($fileHandle, -1, $this->readBytes[$type]);

View File

@ -360,8 +360,7 @@ class Process
do {
$this->checkTimeout();
$running = '\\' === DIRECTORY_SEPARATOR ? $this->isRunning() : $this->processPipes->areOpen();
$close = '\\' !== DIRECTORY_SEPARATOR || !$running;
$this->readPipes(true, $close);
$this->readPipes($running, '\\' !== DIRECTORY_SEPARATOR || !$running);
} while ($running);
while ($this->isRunning()) {
@ -1295,14 +1294,15 @@ class Process
}
$this->processInformation = proc_get_status($this->process);
$running = $this->processInformation['running'];
$this->readPipes($blocking, '\\' === DIRECTORY_SEPARATOR ? !$this->processInformation['running'] : true);
$this->readPipes($running && $blocking, '\\' !== DIRECTORY_SEPARATOR || !$running);
if ($this->fallbackStatus && $this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
$this->processInformation = $this->fallbackStatus + $this->processInformation;
}
if (!$this->processInformation['running']) {
if (!$running) {
$this->close();
}
}