2

I am making a PHP web page that executes a python program and while the python program is running, it displays the output in real-time. After following this answer, this is the code that I am trying.

$cmd = 'python execs/reduction_code.py';
while(@ ob_end_flush());

$proc = popen($cmd, 'r');
echo '<pre class="box">';

while(!feof($proc)){
    echo fread($proc, 4096);
    @ flush();
}

echo '</pre>';
pclose($proc);

After I start executing the program reduction_code.py, in every 30 seconds, it displays few print statements (telling about status of the program). The program takes approx 25 minutes to completely execute. This works well when i run the python program using terminal.

However, when i run this using the above php code, it doesn't show any output at all. The file reduction_code.py is executable and apache has the permission to execute and read it.

Also, when i run a simple python program to ping an address by replacing the value in $cmd as $cmd = 'python execs/test.py'; , I get the expected real-time output of ping probes in my page.

test.py is:

from subprocess import call
call(["ping", "-c", "10", "localhost"])

I am new to PHP and I have no idea why after running reduction.py, the page displays no output at all.

EDIT 1 : When I use $proc = popen($cmd.' 2>&1', 'r'); instead of $proc = popen($cmd, 'r'); to access any error message returned by shell. I get this error message printed on my webpage(at the place where output was expected to appear).

Resource id #9/usr/lib/python2.7/site-packages/IPython/utils/path.py:296: UserWarning: IPython parent '/root' is not a writable location, using a temp directory." using a temp directory."%parent)

/usr/lib64/python2.7/site-packages/astropy/config/configuration.py:687: ConfigurationMissingWarning: Configuration defaults will be used due to OSError:13 on None warn(ConfigurationMissingWarning(msg))
Traceback (most recent call last):
  File "execs/reduction_code.py", line 8, in 
    import matplotlib.pyplot as plt
  File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 26, in 
    from matplotlib.figure import Figure, figaspect
  File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 24, in 
    import matplotlib.artist as martist
  File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 7, in 
    from transforms import Bbox, IdentityTransform, TransformedBbox, \
  File "/usr/lib64/python2.7/site-packages/matplotlib/transforms.py", line 35, in 
    from matplotlib._path import (affine_transform, count_bboxes_overlapping_bbox,
ImportError: /opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by /usr/lib64/python2.7/site-packages/matplotlib/_path.so)

2 Answers 2

2

It sounds like you need to make Python's output unbuffered - this is on by default when running from the command line, but otherwise off. The easiest way of fixing this is to add the -u argument to the hashbang at the start of the script:

#!/usr/bin/python -u

Sign up to request clarification or add additional context in comments.

3 Comments

Tried it. Still no improvement.
How about changing the first line of the PHP code to $cmd = 'python -u execs/reduction_code.py'; ?
Nothing changes even then.
0

I found answer to my question. As error reported in last line of the output :

ImportError: /opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by /usr/lib64/python2.7/site-packages/matplotlib/_path.so)

I found out here that the libraries that lampp is using are too old and does not define GLIBCXX_3.4.9. So, at first I renamed the /opt/lampp/lib/libstdc++.so.6 to /opt/lampp/lib/libstdc++.so.6.orig and then changed the LD_LIBRARY_PATH by changing the command from this :

$proc = popen($cmd.' 2>&1', 'r');

to this:

$proc = popen("LD_LIBRARY_PATH=/usr/lib/ ".$cmd.' 2>&1', 'r');

After I ran my program after this change, another report generated which said that

Runtime Error: could not open display

After doing some search, I found from here and here it was the problem of matplotlib library. I learned that matplotlib library uses X-using backend by default. And X-using backend has problems with few servers. So I followed the answer and inserted these two lines in my reduction_code.py program

import matplotlib
matplotlib.use('Agg')

And now, everything is working as expected. Thank you for support.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.