What happens when we run PHP scripts with long tasks?


I am running a PHP script that is responsible for downloading 55'000 images on one server and downloading these files into a folder on another server. It should take according to statistics per minute around 1h40 to complete the task.

It seems like the script is losing performance as the runtime goes by. The questions are:

  • Does the script lose performance against the server as time passes?
  • Why even closing the tab does the script continue to run?
  • Can I abort script execution and relaunch it?

I know the script is running because by accessing the URL and give F5 the file counter in the directory is modified.

asked by anonymous 04.11.2014 / 15:13

2 answers


Does the script lose performance against the server as time passes?

Depends on the code. Most programmers I know think the Garbage Collector solves any memory issues and eventually causes leaks (this is independent of the platform, and is as relevant to PHP as any other).


Why even closing the tab, does the script continue to roll?

PHP is a server side technology. The client (almost always a browser) makes requests, and the server (Apache / Tomcat / IIS) processes and gives an answer. The response will be compiled and sent to the client's address and port even if the client program is closed.


Can I abort script execution and relaunch it?

It can. The ideal, however, is to perform the long task in parts. For example, handling fifty-five thousand images is a bit heavy, but you can handle twenty in twenty (or maybe even less) and send a response to each picture packet to the client. So you:

  • Ensures that, in case of an error, you can restart the task of the package you stopped in;
  • Transforms the certainty of a timeout into a mere possibility, which can be avoided if the responses are constant;

And the most important:

  • You can clean all the process memory each package, which makes the task of building a scalable system even easier.
04.11.2014 / 15:26

The script itself does not lose performance because it is not changeable after being interpreted by apache (whatever other php server) and compiled, so the code will be the same, which makes it lose the perfomance of it is the way you has built this script and mainly the machine of its server.

If you are using a for loop loop so that you can switch to foreach because each interaction destroys the memory thread and does not overload the server completely, for no, it maintains the allocation of it until the loop is completed.

22.02.2017 / 15:25