PHP Classes

Starting Long Server PHP Scripts and Monitor their Status Part 1: The Theory - Asynchronous Long Server Tasks package blog

Recommend this page to a friend!
  All package blogs All package blogs   Asynchronous Long Server Tasks Asynchronous Long Server Tasks   Blog Asynchronous Long Server Tasks package blog   RSS 1.0 feed RSS 2.0 feed   Blog Starting Long Server ...  
  Post a comment Post a comment   See comments See comments (4)   Trackbacks (0)  


Viewers: 433

Last month viewers: 1

Package: Asynchronous Long Server Tasks

Typical HTTP requests take no more than a few seconds. By default PHP limits script execution to 30 seconds. However, sometimes you need to execute tasks that much longer.

Read this article to learn how to start long tasks without making the user wait and still give progress information of a long task running on the server side.

Loaded Article



The HTTP Request Cycle

Starting a Long Server Task



Most of the times PHP requests are very short. In fact, the shortest are requests the better is the user experience with our Web site. So, why do we want to execute long server tasks?

For certain parts of our web sites, mainly the home page and all indexable pages, the quicker the users receives the response, better our page can be ranked by search engines. Fast sites please the users that will eventually promote the sites in between their friends in social networks.

This is absolutely true, but some tasks often in the administration part of our Web site, need to perform long tasks that can not be finished very quickly. Please, do not take this as a general rule, because most of the pages of our Web site need to be served very quickly.

To make it clear what I am talking about, lets say that we want to post many tweets at once, or upload our 100 pictures to another social network. Those both are tasks that are often are impossible to finish in thirty seconds, right?

In these cases we need to change the set_time_limit in order to use greater values, but there are a few problems changing this kind of PHP settings because if you change it in php.ini all your application will use the new values. Another more serious problem, often you can not change this configuration value in your shared hosting environments.

The purpose of this article is to present an elegant solution that it is is easy and understandable. Additionally you will learn how to monitor the progress of a long task that was started, so the user can see how it is going.

The HTTP Request Cycle

In regular HTTP requests the browser always starts the connection with the server and send a request composed of header lines and body data. Then server prepares the response while the browser is waiting for it, normally in an asynchronous way so the browser does not stop the user to perform any other interactions.

HTTP Request cycle

There are a couple of processes that watch the connection in order to not make the user wait indefinitely. On the browser side there is a timeout period after which it will consider that the request has failed.

On the server side, a standard PHP installation sets a default value of 30 seconds to limit the execution of PHP in order to prevent that the scripts take took long to execute due to bugs.

As mentioned above, sometimes this time limit is not enough to finish a long task. In the diagram above we see that some times we need to have the extra time denoted by the dashed blue lines.

Starting a Long Server Task

When we want to start a long server task from the browser, it is also good to obtain periodically the status of progress of the task.

HTTP Desired Request cycle

Basically we start a request on the server and respond to browser as if the script has finished performing at the beginning, but then continuing working in the long task that we need to perform.

The idea is to disconnect our server script from the HTTP request that started it. We actually start the long task sending another HTTP request that will run the actual long task script.

When the browser sends new requests to obtain the status of the task, we need to provide a way to store and retrieve the status of the started long server tasks. This can be done via local files that the long task updates and the polling requests can check periodically.

We need to queue these tasks and processes one at time in order to not overload and crash the server, but with little changes we could use this system to have all the tasks started by users in parallel.

As you may have guessed, this is a way to emulate threads in your scripts, because you can split the execution in two or more parts running in parallel.


In this part of the article we learned about the theory of how to start long server tasks that can run in parallel without making the user wait, as well how to get the status of those tasks using common storage to let the browser keep polling for status information.

Next part we will learn how to apply this theory in practice using the Asynchronous Long Server Tasks PHP class using the semaphores to queue the long tasks and prevent that too many parallel processes are started on the server.

If you liked this article so far or you have a question about the methods presented here, post a comment here.

You need to be a registered user or login to post a comment

Login Immediately with your account on:


3. Why you have decided to use root directory - troubles with SSL - Giovanni Bartoli (2015-10-05 03:40)
Could be better to reserver a single directory... - 1 reply
Read the whole comment and replies

2. Using cron jobs to as backend scripts - Anthony Amolochitis (2015-09-22 12:41)
Queue external api calls... - 0 replies
Read the whole comment and replies

1. Case of backend scripts - Makhtar Diouf (2015-09-22 04:07)
Case of backend scripts... - 0 replies
Read the whole comment and replies

  Post a comment Post a comment   See comments See comments (4)   Trackbacks (0)  
  All package blogs All package blogs   Asynchronous Long Server Tasks Asynchronous Long Server Tasks   Blog Asynchronous Long Server Tasks package blog   RSS 1.0 feed RSS 2.0 feed   Blog Starting Long Server ...