0

I have a PHP webpage, that makes multiple queries to the database and displays the results on charts.

The logic is, there is index.php, where the query can be made. After submitting the data, 6 different PHP pages are called. The PHP pages log the query, run the appropriate Python script and make charts with Javascript. Each of those 6 PHP pages are displayed in index.php in divs. All of the python scripts have the same input and queried against the same database. Difference comes from the data pulled from the database and also the subsequent Javascript to make the charts.

Example of calling one of the PHP pages:

$("#chartFOO").load("http://example/test/get_foo.php? bar=".concat(bar)+"&start=".concat(start)+"&end=".concat(end), function(responseTxt, statusTxt, xhr){
    if(statusTxt == "error")
        alert("Error: " + xhr.status + ": " + xhr.statusText);
});

Example of calling the Python script:

if ($msisdn) {
    $command = escapeshellcmd("/home/example/scripts/graph_foo.py $bar $start $end");
    $output = shell_exec($command);
}

And the output is then used in the PHP file, to make charts. All of the PHP files are displayed in divs with different styling on index.php.

The problem is, it doesn't run them on multiple threads and locks up the system, which makes the response time for the query quite slow. Is that right, that only one shell command can be ran at a time?!

I have tried putting all the Python scripts as functions and 6 of the PHP files as strings in one file. Trying to call it all with one command, but so far I have problems with formating the PHP files, I can't use '{}' to format, because the PHP files already contain those. Had the idea to use threading module, to run the functions. And use one connection to the database, to save time from connecting 6 times, because it takes time each time.

Is there any reasonable solution to have the scripts run threaded, without having to rework the whole webpage? How can PHP, Javascript and Python be mixed?

A lot to read and a lot to ask, but thanks for advance for your time.

EDIT:

I created a new file, which basically has all the 6 files in it. But calling the Python scripts is a bit different now. And from index.php only calling this one file now, like I did before with 6 files.

Example of new way:

$part->handles = [
    popen("/home/example/scripts/graph_foo.py {$bar} {$start} {$end}", 'r'),
    popen("/home/example/scripts/graph_foo2.py {$bar} {$start} {$end}", 'r')
];

And the way I solved the memory issue:

$output0 = '';
while (!feof($part->handles[0])) {
    $output0 .= fread($part->handles[0], 32768);
}
$output1 = '';
while (!feof($part->handles[1])) {
    $output1 .= fread($part->handles[1], 32768);
}

Don't know if the best way, but works. Don't know PHP well. But it did get 0.5 minutes off the request time, which helps.

  • Does your php script use sessions? PHP locks the session so every next request would have to wait until all previous ones have finished. – jeroen Aug 08 '18 at 13:20
  • Yeah it does, the index.php has session control at the start and so do the 6 PHP files. That is why it would be good if I knew how to consolidate the 6 files to 1 file – veteran007 Aug 08 '18 at 13:23
  • Perhaps this could work for you: https://stackoverflow.com/a/41338163/42139 – jeroen Aug 08 '18 at 13:31
  • I am a bit confused, I call the scripts from 6 different PHP files. If as you said, I can't make multiple requests at the same time, if the session is involved. How does it help. I can still only run one PHP file at a time then. – veteran007 Aug 08 '18 at 13:45
  • No, you would need 1 php script only to make multiple requests to your python scripts. – jeroen Aug 08 '18 at 16:15
  • I am back, trying to make it all work. I did as you said, but now I am running into an issue, where fread() needs bytes allocated to it, but even changing the php.ini `memory_limit=-1` It still has some 128M limit on it. The data sets are quite big, it gets even larger if the asked time period gets longer. – veteran007 Aug 09 '18 at 09:15

0 Answers0