3

I have a webpage that when users go to it, multiple (10-20) Ajax requests are instantly made to a single PHP script, which depending on the parameters in the request, returns a different report with highly aggregated data.

The problem is that a lot of the reports require heavy SQL calls to get the necessary data, and in some cases, a report can take several seconds to load.
As a result, because one client is sending multiple requests to the same PHP script, you end up seeing the reports slowly load on the page one at a time. In other words, the generating of the reports is not done in parallel, and thus causes the page to take a while to fully load.

Is there any way to get around this in PHP and make it possible for all the requests from a single client to a single PHP script to be processed in parallel so that the page and all its reports can be loaded faster?

Thank you.

3 Answers 3

3

As far as I know, it is possible to do multi-threading in PHP. Have a look at pthreads extension.

What you could do is make the report generation part/function of the script to be executed in parallel. This will make sure that each function is executed in a thread of its own and will retrieve your results much sooner. Also, set the maximum number of concurrent threads <= 10 so that it doesn't become a resource hog.

Here is a basic tutorial to get you started with pthreads.

And a few more examples which could be of help (Notably the SQLWorker example in your case)

2
  • Thank you. If I were to use this, then would I want to make a single Ajax call to the PHP script and then like you said create the threads in the PHP script to make the DB queries and generate the reports? Assuming that that is the best thing to do, is it possible to render each of the reports out to the screen as they are generated, or do I need to generate all the reports and then return them all at once in a single echo statement?
    – HartleySan
    Aug 23, 2015 at 14:33
  • This depends on the way you handle your ajax calls. Lets assume you use a different function for retrieving each report. Then your multiple ajax calls would create multiple threads, and the main thread will wait for all the child threads to finish and return so that you can echo all of your reports at once. That's the easiest implementation. You can also have the child threads echo their result as soon as they finish (but you might have to make some design changes for this to work. The links to the tutorials will help you achieve this) Aug 23, 2015 at 16:35
1

Server setup

This is more of a server configuration issue and depends on how PHP is installed on your system: If you use php-fpm you have to increase the pm.max_children option. If you use PHP via (F)CGI you have to configure the webserver itself to use more children.

Database

You also have to make sure that your database server allows that many concurrent processes to run. It won’t do any good if you have enough PHP processes running but half of them have to wait for the database to notice them.

In MySQL, for example, the setting for that is max_connections.

Browser limitations

Another problem you’re facing is that browsers won’t do 10-20 parallel requests to the same hosts. It depends on the browser, but to my knowledge modern browsers will only open 2-6 connections to the same host (domain) simultaneously. So any more requests will just get queued, regardless of server configuration.

Alternatives

If you use MySQL, you could try to merge all your calls into one request and use parallel SQL queries using mysqli::poll().

If that’s not possible you could try calling child processes or forking within your PHP script.

1

Of course PHP can execute multiple requests in parallel, if it uses a Web Server like Apache or Nginx. PHP dev server is single threaded, but this should ony be used for dev anyway. If you are using php's file sessions however, access to the session is serialized. I.e. only one script can have the session file open at any time. Solution: Fetch information from the session at script start, then close the session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.