Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 891
Kenny Wyland Avatar asked Feb 26 '20 23:02

Kenny Wyland


People also ask

How does PHP handle multiple requests?

Requests are handled in parallel by the web server (which runs the PHP script). Updating data in the database is pretty fast, so any update will appear instantaneous, even if you need to update multiple tables.

How many concurrent requests can PHP handle?

PHP Apache - Handle 1000 simultaneous request.


2 Answers

This is because the PHP server only uses a single process by default, which prevents it from executing multiple requests simultaneously.

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.

like image 146
Pieter van den Ham Avatar answered Sep 18 '22 15:09

Pieter van den Ham


The built in web server is a single-threaded, single-process, synchronous server. It's only meant for testing purposes. It can only handle a single request at a time. It cannot serve multiple concurrent requests at the same time.

like image 29
Sherif Avatar answered Sep 18 '22 15:09

Sherif