php / php-src

The PHP Interpreter
https://www.php.net
Other
38.19k stars 7.75k forks source link

Support PHP_CLI_SERVER_WORKERS on Windows #12071

Open madelson opened 1 year ago

madelson commented 1 year ago

Description

The docs say that this mechanism doesn’t work on Windows, but this would be just as helpful to Windows devs.

iluuu1994 commented 1 year ago

Hi @madelson! PHP_CLI_SERVER_WORKERS is implemented using fork. Windows doesn't have fork, so this would have to be implemented using threads. However, threads are only possible with a ZTS build, which is not how PHP is usually built for Linux. Maintaining both a fork and threads mechanism doesn't seem worth it for a convenience web server that should only be used in development.

What's your use case? Are you experiencing real performance issues in development?

madelson commented 1 year ago

@iluuu1994 it makes sense that this is not trivial if the Linux implementation uses fork().

What's your use case? Are you experiencing real performance issues in development?

I'm doing development in a Windows environment on a Laravel app. The app makes various AJAX requests to the server, some of which can be slow because they are calling out to 3rd-party APIs. With the current model, everything queues up behind the slow requests, making for a frustrating and unrealistic developer experience.

this would have to be implemented using threads

I'm new to PHP and don't know much about the threading model yet. I wonder if you couldn't still leverage multiple worker processes (instead of threads) on windows, similar to how IIS is able to hand off incoming requests to a worker process pool.

Like I said, new to PHP so if you can recommend a typical Windows workaround (presumably running behind some more production-like server) that would also be valuable to me :-).

iluuu1994 commented 1 year ago

@madelson Fair enough, let's keep this open then.

madelson commented 1 year ago

Thanks @iluuu1994 !

Not sure if there would be issues with this, but what if, in the presence of the environment variable, it just spun up a minimal server that then would launch N instances of today's single-threaded server as worker processes and forward requests in a round-robin manner?

iluuu1994 commented 1 year ago

@madelson Unfortunately it's not quite that simple. We're using a shared network socket. It is created before forking. Unix will then forward a network request to all of the processes, and the fastest will accept it. There's currently no other means of communication between the processes. Without this mechanism, we need to forward the requests from the master process to the workers.

Sebjugate commented 1 year ago

Another use-case for this is when running SSE (or other long polling) that request completely blocks all others. It would be very convenient to be able to test and develop such an application with the convenience of the built-in web server.

Sebjugate commented 1 year ago

Maybe this helps someone.

I made this PowerShell script that uses Caddy to load balance between several PHP processes as a workaround for now. Seems to work 🤷

$processes = @()
$ports = 8081..8085
$ports | ForEach-Object {
    $processes += Start-Process php -PassThru -ArgumentList "-S 127.0.0.1:$_ -t public public/index.php" -NoNewWindow
}
$upstreams = ($ports | ForEach-Object { "--to=:$_" })
.\caddy.exe reverse-proxy --from :8080 @upstreams
$processes | ForEach-Object { Stop-Process $_.Id }
yuppox commented 7 months ago

I would also like to see this feature.