2

I'm working on a project for my company. Essentially we are going to have a long running process that will hit APIs on a python server to send and recieve JSON responses. It will take the data from these API calls and update tables in a mysql database. It will be responsive, but also perform scheduled tasks.

The code is written and it functions properly. The question is, how do you execute this intelligently? While being memory conscious.

Much like this question I am at the moment, very regretfully and in spite of my many reservations, using a while(true) loop to run infinitely with a sleep of 15 seconds after everything finishes. The referenced question didn't have very many viable answers and is nearly 3 years old. Any suggestions on how to execute this in a way that will be memory friendly and intelligent.

Community
  • 1
  • 1
Allie Fitter
  • 1,689
  • 14
  • 18
  • Back in the mainframe days, CICS required a programming style where the program would terminate and restart when required with the question "Now, what was I doing?" It proved to be highly scalable so you might want to consider this approach if you run into resource problems with the simpler one. – Mike Apr 12 '16 at 22:20
  • Consider checking out a project like PHP-Daemon - https://github.com/shaneharter/PHP-Daemon - "Create solid, long-running PHP daemon processes by extending the Core_Daemon class." "Create conventional single-process applications or choose true parallel processing in PHP with persistent background workers." – Ryan Rentfro Apr 13 '16 at 15:55

3 Answers3

0

If you are worried about leaking memory while your script runs, you could move the infinite loop to a wrapper script that simply calls your script. Something like:

<?php
while(1) {
  exec("worker_script.php");
  sleep(15);
} 
ChrisRibe
  • 168
  • 5
0

The question is, how do you execute this intelligently? While being memory conscious.

Doesn't really sound like much is wrong with this approach. If you want to make sure you don't have any memory leaks, make sure you allocate all of your objects with a local scope to ensure it doesn't accumulate to cause an out-of-memory error.

I would also recommend a daemon supervisor something like Supervisor to auto-restart your PHP process if it crashes.

An alternative to this entire approach is to remove your while loop and auto-launch the PHP process (via CRON job or similar) every few minutes, and let it exit as soon as it's done. The problem with this is that now you have to worry about preventing two or more of your scripts from running in parallel.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
0

You might want to avoid sleep(). It times out and can crash, hence Martin's comment about using Supervisor or another process that restarts scripts. This script avoids sleep and memory use is low.

defined("RESTART_TIMER") || define("RESTART_TIMER", 15);
$restartTime = time() + RESTART_TIMER;
for (;;) {
   // Most efficient loop possible. 
   if (time() >= $restartTime) {
       exec("yourFile.php");
       $restartTime = time() + RESTART_TIMER;
   }
}

If you run php -a, you can try it by pasting in this:

defined("RESTART_TIMER") || define("RESTART_TIMER", 1);
$restartTime = time() + RESTART_TIMER;
for (;;) {
   // Most efficient loop possible. 
   if (time() >= $restartTime) {
       echo memory_get_usage(true) . PHP_EOL;
       $restartTime = time() + RESTART_TIMER;
   }
}

Hit ctrl-c to quit it when you get tired, but the memory usage is constant.

Michael Ryan Soileau
  • 1,763
  • 17
  • 28