What happens when we run PHP scripts with long tasks?

Asked

Viewed 160 times

7

I’m running a script PHP who is responsible for downloading 55'000 images on one server and downloading these files into a folder on another server. It should take statistics per minute about 1.40 a.m. to complete the task.

It seems that the script will lose performance as the running time passes. The questions are:

  • Script loses performance over server as time passes?
  • Because even closing the tab, the script keeps running?
  • I can somehow abort script execution and relaunch it?

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

2 answers

12


Script loses performance over server as time passes?

It depends on the code. Most programmers I know think that Garbage Collector solves any problem with memory and ends up causing leaks (this is independent of the platform, and is as relevant in PHP as any other).

Because even closing the tab, the script keeps running?

PHP is a server side technology. The client (almost always a browser) makes requests, and the server (Apache/Tomcat/IIS) processes and responds. The answer will be prepared and sent to the address and door of the client even if the client program is closed.

I can somehow abort script execution and relaunch it?

Yes. The ideal, however, is to perform the long task in parts. For example, treating fifty-five thousand images is a bit heavy, but you can treat it every twenty (or maybe even less) and send a response to each pack of images to the customer. So you:

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

And most importantly:

  • You can thus wipe all the memory consumed by the process to each package, which makes it even easier the task of building a scalable system.

0

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

If you are using a loop for guide you to change to foreach because the same to each interaction destroys the thread of the memory and does not totally overload the server, it is no, it keeps the allocation of the same until the loop is complete.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.