Author: Joseluis Laso
Posted on: 2015-09-22
Package: Asynchronous Long Server Tasks
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.
Contents
Introduction
The HTTP Request Cycle
Starting a Long Server Task
Conclusion
Introduction
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
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.
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.
Conclusion
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
1,353,264 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
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