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

Add loop mode (-L) in the CLI SAPI #2

Open
wants to merge 7 commits into
base: PHP-7.2
Choose a base branch
from
Open

Conversation

mnapoli
Copy link
Owner

@mnapoli mnapoli commented Dec 7, 2018

This is a pull request that I opened in my own fork to preview the diff and allow code review.

This PR adds a new "Loop" mode that can be used with -L. For example: php -L script.php.

This mode runs the script in a loop.

The big difference with doing a while(true) {} loop in PHP is that every execution of the script is isolated from the other executions.

It's basically a clone of the PHP-FPM (worker) behavior for CLI.

Use cases

  • Workers, i.e. processing jobs in a message queue: instead of running in a loop the script can process one job, the "Loop" mode will ensure the script will loop => each job will be processed in a fresh PHP environment, meaning no memory leaks, no shared state, etc.
  • AWS Lambda: by running php -L each event/request would be processed in an isolated PHP environment without having the overhead of starting a new process: see Scenario G: custom PHP SAPI bref-bootstrap-benchmarks#14

But wait, we can already do that by keeping the PHP script alive to handle multiple requests/jobs? (like Laravel Octane, Swoole, RoadRunner, etc.)

Yes! It's very similar. Here is a comparison:

"Loop" mode (this PR) Keeping the PHP script alive
Saves the time needed to start PHP
Saves the time to bootstrap the application/kernel
Can be used on any PHP app without changes
No risks related to memory leaks/cleaning up global state

Example

Before:

<?php
// script.php
$a = 'foo';
while (true) {
    echo $a . PHP_EOL;
    // the global state can be changed
    // and it will impact next executions
    $a = 'bar';
}
$ php -L script.php
foo
bar
bar
[...]

With the "loop mode":

<?php
// script.php
$a = 'foo';
echo $a . PHP_EOL;
// the global state can be changed
// but it will not impact next executions
$a = 'bar';
$ php -L script.php
foo
foo
foo
[...]

Understanding how it works

This is pseudo code of the new -L mode:

while (is_running) {
        // Boot the PHP engine
	php_request_startup();

        // Execute `index.php` in the current process
        php_run_script("index.php");

        // Reset the PHP engine (clear variables, memory, open resources, etc.)
        php_request_shutdown();
}

@deleugpn
Copy link

This is ironically funny that the PHP Internal Server is limited 1 request at a time (no support for parallel request) but AWS Lambda has the exact same limitation, which makes it an aligned match!

@determin1st
Copy link

how to exit?

@mnapoli
Copy link
Owner Author

mnapoli commented Oct 25, 2021

@determin1st you can't. Just like you can't "exit" the whole PHP-FPM process when running inside a PHP-FPM worker.

@determin1st
Copy link

@determin1st you can't. Just like you can't "exit" the whole PHP-FPM process when running inside a PHP-FPM worker.

i need to exit. https://github.com/determin1st/sm-bot/blob/c162330e92219b5c71373b21b59ddade8aaa5bb2/start.bat#L18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants