File tree Expand file tree Collapse file tree 3 files changed +47
-2
lines changed
src/Symfony/Component/Process Expand file tree Collapse file tree 3 files changed +47
-2
lines changed Original file line number Diff line number Diff line change 1
1
CHANGELOG
2
2
=========
3
3
4
+ 7.1
5
+ ---
6
+
7
+ * Add support for ` Fiber `
8
+
4
9
6.4
5
10
---
6
11
Original file line number Diff line number Diff line change @@ -429,12 +429,31 @@ public function wait(?callable $callback = null): int
429
429
do {
430
430
$ this ->checkTimeout ();
431
431
$ 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
+ }
433
443
} while ($ running );
434
444
435
445
while ($ this ->isRunning ()) {
436
446
$ 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
+ }
438
457
}
439
458
440
459
if ($ this ->processInformation ['signaled ' ] && $ this ->processInformation ['termsig ' ] !== $ this ->latestSignal ) {
Original file line number Diff line number Diff line change @@ -587,6 +587,27 @@ public function testMustRun()
587
587
$ this ->assertEquals ('foo ' .\PHP_EOL , $ process ->getOutput ());
588
588
}
589
589
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
+
590
611
public function testSuccessfulMustRunHasCorrectExitCode ()
591
612
{
592
613
$ process = $ this ->getProcess ('echo foo ' )->mustRun ();
You can’t perform that action at this time.
0 commit comments