Skip to content
Advertisement

Multiple php requests simultaneously, second request doesn’t start until first finishes

I’ve got a long running php 7.2 script that is producing a zip file. I want to use a looping ajax call to check on the progress of building the zip file. The second script appears to be locked and doesn’t start processing until the first script is completely done.

In fact, the second script doesn’t even print the error_log() on line 1 of my index.php routing script until after the first script is completely finished.

The top of my index.php router script:

<?
error_log('top of index '.$_SERVER['REQUEST_URI']);

This is true even if I’m just requesting static image resources. The error_log() on line 1 doesn’t even print until after the long-running script has completely finished.

At first, I believed I was running into Session Locking as described here but the solution they offer doesn’t seem to work (calling session_write_close()), and I’m wondering if something else is going on because the second script is locking before line 1 as opposed to locking up when I try to start the session. The second script doesn’t seem to be starting AT ALL. I thought maybe the server was automatically starting the session before line 1, but I checked and my server has session.auto_start=0.

Is there some server configuration I need to set?

What am I missing? What am I doing wrong?

I’m running this on localhost (Mac) with the built-in PHP server.

php -c /usr/local/etc/php/7.2/php.ini -S 127.0.0.1:8080 index.php

Advertisement

Answer

The built-in server supports multiple workers from PHP 7.4 onward (on platforms where fork() is available). See this commit: https://github.com/php/php-src/commit/82effb3fc7bcab0efcc343b3e03355f5f2f663c9

To use it, add the PHP_CLI_SERVER_WORKERS environment variable before starting the server. For example:

PHP_CLI_SERVER_WORKERS=10 php7.4 -S 127.0.0.1:7080 index.php

I recommend upgrading your PHP 7.2 installation if you want to utilize this feature.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement