0

One of my php scripts (setup with php-fpm 7.0 with nginx) needs high cpu usage (legitimately) to complete. I would like it to take more time to complete, but leave the cpu available for other requests. Currently the server is not responsive while that script is running.

I could create some delays with PHP sleep(), but is there a more appropriate method for load balancing?

0

You might be looking for nice command in Linux/UNIX. Basically, what it does is running a command with a modified scheduling priority. PHP sleep() might not help if running task is heavy by itself - without repeating.

  • How can I setup it with ngjnx? – Michael Chourdakis Apr 23 '17 at 12:15
  • That is not related to nginx: you spawn another process with exec() or system(). Command (executed by these) would be something like nice -n{level-of-niceness} php /path/to/your-script.php – Miloš Đakonović Apr 23 '17 at 12:18
0

You can use sleep (int $seconds) to pause script execution. Also there exists microseconds version: usleep (int $micro_seconds).

Using sleep or usleep will stop your script leaving the CPU available for other processes.

Also there is an utility called nice. This util controls process priority for CPU. You can use nice -n 19 php script.php to reduce script priority to the lowest value. nice priority is -20 (max priority) to +19 (min priority).

I think the best solution for you will be sleep or usleep because this is very simple solution and will not require any modifications to web server and application server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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