Skip to content

Commit 1c5b2ee

Browse files
committed
[Process] Add support for Fiber
1 parent 9e810de commit 1c5b2ee

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/Symfony/Component/Process/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Add support for `Fiber`
8+
49
6.4
510
---
611

src/Symfony/Component/Process/Process.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,31 @@ public function wait(?callable $callback = null): int
429429
do {
430430
$this->checkTimeout();
431431
$running = $this->isRunning() && ('\\' === \DIRECTORY_SEPARATOR || $this->processPipes->areOpen());
432-
$this->readPipes($running, '\\' !== \DIRECTORY_SEPARATOR || !$running);
432+
if ($fiber = \Fiber::getCurrent()) {
433+
$this->readPipes(false, '\\' !== \DIRECTORY_SEPARATOR || !$running);
434+
$startedAt = microtime(true);
435+
$fiber->suspend();
436+
$sleepFor = (int) (1000 - (microtime(true) - $startedAt) * 1000000);
437+
if (0 < $sleepFor) {
438+
usleep($sleepFor);
439+
}
440+
} else {
441+
$this->readPipes($running, '\\' !== \DIRECTORY_SEPARATOR || !$running);
442+
}
433443
} while ($running);
434444

435445
while ($this->isRunning()) {
436446
$this->checkTimeout();
437-
usleep(1000);
447+
if ($fiber = \Fiber::getCurrent()) {
448+
$startedAt = microtime(true);
449+
$fiber->suspend();
450+
$sleepFor = (int) (1000 - (microtime(true) - $startedAt) * 1000000);
451+
if (0 < $sleepFor) {
452+
usleep($sleepFor);
453+
}
454+
} else {
455+
usleep(1000);
456+
}
438457
}
439458

440459
if ($this->processInformation['signaled'] && $this->processInformation['termsig'] !== $this->latestSignal) {

src/Symfony/Component/Process/Tests/ProcessTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,27 @@ public function testMustRun()
587587
$this->assertEquals('foo'.\PHP_EOL, $process->getOutput());
588588
}
589589

590+
public function testMustRunWithFiber()
591+
{
592+
$process = new Process(['sleep', 1]);
593+
$fiber = new \Fiber(function () use ($process) {
594+
$process->mustRun();
595+
});
596+
$fiber->start();
597+
598+
$fiberHasBeenSuspended = false;
599+
while (!$fiber->isTerminated()) {
600+
$fiberHasBeenSuspended = true;
601+
$this->assertTrue($fiber->isSuspended());
602+
$fiber->resume();
603+
604+
usleep(900);
605+
}
606+
607+
$this->assertTrue($process->isTerminated());
608+
$this->assertTrue($fiberHasBeenSuspended);
609+
}
610+
590611
public function testSuccessfulMustRunHasCorrectExitCode()
591612
{
592613
$process = $this->getProcess('echo foo')->mustRun();

0 commit comments

Comments
 (0)