Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically run Loop at end of program (autorun) #232

Merged
merged 3 commits into from
Jul 3, 2021

Conversation

clue
Copy link
Member

@clue clue commented Jun 30, 2021

When using the Loop class, it will automatically execute the loop at the end of
the program. This means the following example will schedule a timer and will
automatically execute the program until the timer event fires:

use React\EventLoop\Loop;

Loop::addTimer(1.0, function () {
    echo 'Hello' . PHP_EOL;
});

As of v1.2.0, we highly recommend using the Loop class this way and omitting any
explicit run() calls. For BC reasons, the explicit run()
method is still valid and may still be useful in some applications, especially
for a transition period towards the more concise style.

If you don't want the Loop to run automatically, you can either explicitly
run() or stop() it. This can be useful if you're using
a global exception handler like this:

use React\EventLoop\Loop;

Loop::addTimer(10.0, function () {
    echo 'Never happens';
});

set_exception_handler(function (Throwable $e) {
    echo 'Error: ' . $e->getMessage() . PHP_EOL;
    Loop::stop();
});

throw new RuntimeException('Demo');

This is a pure feature addition that works across all loops and does not affect BC.

Builds on top of #226, #229 and #231

@clue clue added this to the v1.2.0 milestone Jun 30, 2021
@clue clue force-pushed the loop-autorun branch 2 times, most recently from da3412f to 4b7727e Compare June 30, 2021 14:24
Copy link
Contributor

@kelunik kelunik left a comment

Choose a reason for hiding this comment

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

We've previously briefly discussed this, and I'd recommend against introducing this. Application registered shutdown functions won't be run if they're registered inside event loop watchers and a fatal error or explicit exit; happen, see e.g. https://3v4l.org/duM4S.

@clue
Copy link
Member Author

clue commented Jun 30, 2021

We've previously briefly discussed this, and I'd recommend against introducing this. Application registered shutdown functions won't be run if they're registered inside event loop watchers and a fatal error or explicit exit; happen, see e.g. https://3v4l.org/duM4S.

The autorun functionality is a new feature that will only be used when switching to the Loop class. Existing code explicitly using the $loop->run() call or new code explicitly switching to Loop::run() can still explicitly run the loop without resorting to the autorun functionality. If this is a concern for your use case, the loop would not be executed from within a shutdown function in this case.

Other than that, I don't see how this would affect real-world use cases in any negative way. I've added a bunch of test cases that verify common application setups work as expected (either using the new style or the existing style). If you feel anything's missing, I'm happy to look into this and provide additional test cases 👍

@clue
Copy link
Member Author

clue commented Jul 1, 2021

Rebased on master to resolve merge conflict now that #231 is in :shipit:

@clue clue requested a review from WyriHaximus July 3, 2021 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants