Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Process] Add feature "wait until callback" to process class#27742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:masterfromNek-:feature/process-wait-until
Oct 11, 2018

Conversation

@Nek-
Copy link
Contributor

@Nek-Nek- commentedJun 27, 2018
edited
Loading

I often see code like the following:

$process->start();// wait for the process to be readysleep(3);

Many times in tests, sometimes in other places. There is a problem with this kind of code because if for any reason the process starts slower than the usual... Your code may fail after that. Also if it's faster, you're losing time for waiting.

So here is my proposal:

$process->start();$process->waitUntil(function($type,$output) {// check the output and return true to stop waiting when you got what you wait});
QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsnone
LicenseMIT
Doc PRWaiting for feedbacks

Awkan, pimolo, Kocal, athos7933, andreybolonin, and mcsky reacted with thumbs up emoji
@Nek-Nek-force-pushed thefeature/process-wait-until branch fromac00c28 to29b946cCompareJune 27, 2018 13:12
@nicolas-grekasnicolas-grekas added this to thenext milestoneJun 27, 2018
@Nek-Nek- changed the titleAdd feature "wait until callback" to process class[Process] Add feature "wait until callback" to process classJun 27, 2018
$p->stop();

$this->assertEquals("First iteration output\nSecond iteration output\nOne more iteration output\n",$completeOutput);
$this->assertLessThan(15,microtime(true) -$start);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

15 is too much, the process do 3 iterations inKillableProcessWithOutput.php so it should take less than 2 seconds

@Nek-Nek-force-pushed thefeature/process-wait-until branch 2 times, most recently from9b4a44f tobb56dc2CompareJune 27, 2018 14:19

foreach ($outputsas$output) {
usleep($iterationTime);
$iterationTime *=10;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Won't this create an extremely slow test?

Copy link
ContributorAuthor

@Nek-Nek-Jun 27, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The test should end after1s and 110ms of execution of this code. If a failure occurs, the timeout will be reached 60s after the start of the test.

The process is long to end (but ends, for "security" purpose) to simulate the running of a daemon. It is stop by the test.

Note: for some reason under windows it looks like this test takes 4s. No idea why and how. Any though about?

Windows PHPUnit output on test process:

1) Symfony\Component\Process\Tests\ProcessTest::testWaitUntilSpecificOutputFailed asserting that 4.3028318881989 is less than 2.C:\projects\symfony\src\Symfony\Component\Process\Tests\ProcessTest.php:155

Here is anugly fix suggestion.

@Nek-Nek-force-pushed thefeature/process-wait-until branch from5339b83 to0f428ebCompareJune 27, 2018 16:13
@Nek-
Copy link
ContributorAuthor

Hello@iltar what's the state of this PR? :)

@linaori
Copy link
Contributor

@Nek- it's your PR, so I'm not sure what you mean 😅

@Nek-
Copy link
ContributorAuthor

@iltar sorry you're so active on PRs that I thought you were in the core team 😅!

linaori reacted with laugh emoji

@Nek-
Copy link
ContributorAuthor

ping@romainneutron@nicolas-grekas then :)

@Nek-Nek-force-pushed thefeature/process-wait-until branch 2 times, most recently from4df8f2f tof6b12e0CompareOctober 5, 2018 07:41
@Nek-
Copy link
ContributorAuthor

Nek- commentedOct 5, 2018

Annnnnd... Rebased again. ping@romainneutron@nicolas-grekas

@Nek-
Copy link
ContributorAuthor

Nek- commentedOct 5, 2018

Error on CI not related.

}

/**
* Waits until the callback return true.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

typo: returns


if (!$this->processPipes->haveReadSupport()) {
$this->stop(0);
thrownew \LogicException('Pass the callback to the Process::start method or enableOutput to use a callback with Process::waitUntil');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Can you add a dot at the end of the exception message? Also, you need to quote PHP code:

Pass the callback to the "Process::start" method or "enableOutput" to use a callback with "Process::waitUntil".

Perhaps addcall before enableOutput:... method or call "enableOutput" ...

@fabpot
Copy link
Member

Can you also add some notes in the CHANGELOG file of the component?

returnfunction ($type,$data)use ($callback) {
if (null !==$callback) {
\call_user_func($callback,$type,$data);
return$callback($type,$data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

does this finally work forall kind of callables in PHP 7.1+ ? In PHP 5.6, I know there was still 1 kind of callable failing this.

Copy link
ContributorAuthor

@Nek-Nek-Oct 11, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I was not aware of that fact. No idea. Can you give more insights to verify that fact?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Let's revert this change, no big deal (same below)

@Nek-Nek-force-pushed thefeature/process-wait-until branch fromf6b12e0 to0617230CompareOctober 11, 2018 06:58
returnfunction ($type,$data)use ($callback) {
if (null !==$callback) {
\call_user_func($callback,$type,$data);
return$callback($type,$data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Let's revert this change, no big deal (same below)

@Nek-Nek-force-pushed thefeature/process-wait-until branch from84d938d to01eb0d5CompareOctober 11, 2018 13:41
@Nek-
Copy link
ContributorAuthor

@fabpot changes done.

@fabpotfabpotforce-pushed thefeature/process-wait-until branch from01eb0d5 to27eaf83CompareOctober 11, 2018 17:22
@fabpot
Copy link
Member

Thank you@Nek-.

@fabpotfabpot merged commit27eaf83 intosymfony:masterOct 11, 2018
fabpot added a commit that referenced this pull requestOct 11, 2018
… class (Nek-)This PR was squashed before being merged into the 4.2-dev branch (closes#27742).Discussion----------[Process] Add feature "wait until callback" to process classI often see code like the following:```php$process->start();// wait for the process to be readysleep(3);```Many times in tests, sometimes in other places. There is a problem with this kind of code because if for any reason the process starts slower than the usual... Your code may fail after that. Also if it's faster, you're losing time for waiting.So here is my proposal:```php$process->start();$process->waitUntil(function($type, $output) {    // check the output and return true to stop waiting when you got what you wait});```| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | none| License       | MIT| Doc PR        | Waiting for feedbacksCommits-------27eaf83 [Process] Add feature \"wait until callback\" to process class
@Nek-Nek- deleted the feature/process-wait-until branchOctober 12, 2018 07:48
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull requestOct 16, 2018
This PR was merged into the master branch.Discussion----------Add some lines of doc for waitUntil() (Process)This adds some documentation forsymfony/symfony#27742Commits-------2814fea Add some lines of doc for waitUntil() (Process)
@miniyarov
Copy link
Contributor

do {            $this->checkTimeout();            $running = '\\' === \DIRECTORY_SEPARATOR ? $this->isRunning() : $this->processPipes->areOpen();            $output = $this->processPipes->readAndWrite($running, '\\' !== \DIRECTORY_SEPARATOR || !$running);             foreach ($output as $type => $data) {                if (3 !== $type) {                    $wait = !$callback(self::STDOUT === $type ? self::OUT : self::ERR, $data);                } elseif (!isset($this->fallbackStatus['signaled'])) {                    $this->fallbackStatus['exitcode'] = (int) $data;                }            }        } while ($wait);

This loop uses CPU %100 while waiting for the output. Because getting process information comes with a cost.
For this reason it's a good idea to put at least ausleep(1000) in loop, to minimize cpu usage.
As done inwait functionhttps://github.com/symfony/process/blob/master/Process.php#L421

@nicolas-grekas
Copy link
Member

Would you mind sending a PR doing so?

@miniyarov
Copy link
Contributor

Yes, forgot to mention it here#28940

@miniyarov
Copy link
Contributor

@nicolas-grekas fyi

@nicolas-grekasnicolas-grekas modified the milestones:next,4.2Nov 1, 2018
This was referencedNov 3, 2018
$output =$this->processPipes->readAndWrite($running,'\\' !== \DIRECTORY_SEPARATOR || !$running);

foreach ($outputas$type =>$data) {
if (3 !==$type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Hey@Nek- ! Happy New Year! I'm a bit late to the party but… could you please explain what this magic number is about? I thought possible values would be 1 or 2, but I didn't expect 3… any pointers? I'm trying to investigate this failure:https://ci.appveyor.com/project/fabpot/symfony/builds/21320382#L1210

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This issue is not related to this condition (or doesn't look related to me). Basically, if you extend the timeout for windows only, it may fix the issue. It looks like windows is slooooooow (not that slower on my computer though) to execute a process.

But I can't remember why I used$type !== 3, sorry.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Not sure it has to do with Windows being slow, because if you look, there is the beginning of the output, and then the end of it, but the middle is missing 😅 :

First iteration output\nOne more iteration output\n

The code of the test is pretty straightforward, that's why I'm starting to suspect the code might be at fault, maybe here or inWindowsPipes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

3 seems to be the index of a pipe that will receive the last exit code, and indeed, does not seem to have much to do with the problem at hand because it is does not seem to be used on Windows.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@stofstofstof left review comments

@fabpotfabpotfabpot approved these changes

+3 more reviewers

@greg0iregreg0iregreg0ire left review comments

@linaorilinaorilinaori left review comments

@mcskymcskymcsky left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.2

Development

Successfully merging this pull request may close these issues.

10 participants

@Nek-@linaori@fabpot@miniyarov@nicolas-grekas@stof@greg0ire@mcsky@xabbuh@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp