0

I have written the following test php code that will run a python script, note that there are 3 methods listed (all work). The last method will run the python script but does not cause the PHP page to wait for it to finished.

 <?PHP
  $SKU= 'asdfghyt42';
  $showID = '60';


  //method 1
  $command='C:\Python27\python.exe python\pythontesting.py $SKU $showID';
  $buffer='';
  ob_start(); // prevent outputting till you are done
   passthru($command);
  //get the result out
 $sketchfabKey=ob_get_contents();
  //clean up the buffer
 ob_end_clean();
 echo $sketchfabKey; 


 /*
 /method 2
 $command = escapeshellcmd('C:\Python27\pythonw.exe python\pythontesting.py $SKU $showID');
$output = shell_exec($command);
echo $output;


 // method 3
$command="C:\Python27\pythonw.exe python\pythontesting.py $sketchfabKey $SKU"; 
$out = 0;
ob_end_clean();
ignore_user_abort();
ob_start();
header("Connection: close");
echo json_encode($out);
header("Content-Length: " . ob_get_length());
ob_end_flush();
flush();
  exec($command);
  */

 ?>

the python script will open a windows messagebox and tell you it is python script 1

import win32api
import sys
import subprocess

win32api.MessageBox(0, '1:', 'yay', 0x00001000)
p = subprocess.call([sys.executable, 'pythontesting2.py'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print "I am alive"

Once the user clicks ok button on the message box it should run the next python script which does the same thing with the message box stating it is script 2

import win32api
import sys
import subprocess

win32api.MessageBox(0, '2:', 'yay', 0x00001000)
p = subprocess.call([sys.executable, 'pythontesting3.py'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print "I am alive"

This is where the problem occurs. If I run the 1st python script from the python shell it works exactly as intended and give me a messagebox with 1 and then a messagebox with 2.

However when the first python script is executed by the PHP page, python will only execute code within the 1st script and give me messagebox with a 1. When I click ok I do not get the 2nd messagebox and thus have concluded that the 2nd python script is never started.

I thought it may be a permissions issue with windows, set the python.exe's to run as administrator and made sure that all permissions were set to full control. It did not change anything.

Here is some info on the system if that will help:
Python 2.7
webserver is run locally via Apache and setup with XAMPP

  • 1
    Escape your back-slashes! `C:\Python27` should be `C:\\Python27` for instance. And never use relative paths when excecuting something.. `python\\pythontesting.py` could be anywhere, and you never know which folder the PHP runs out of let alone Python under PHP.. Will be it running in `C:\Python27` or your web dir? Better to use absolute paths for **everything**. And as a final note, `$SKU` won't get resolved because it's within `'` and not `"` – Torxed Jan 04 '17 at 23:13
  • And I'm not PHP guru, but `exec()` is probably better than `passthru()` – Torxed Jan 04 '17 at 23:16
  • Final thought, `subprocess.call` **probably** hangs your application because it's waiting for a process to finish or something, try switching to [popen.poll()](http://stackoverflow.com/questions/41462957/python-subprocess-communicate-freezes-when-reading-output?noredirect=1#comment70134378_41462957) which got answers a few hours ago. And why are you starting `pythontesting3.py` via `subprocess.call` to begin with? Why not just do `import pythontesting3`? It's a Python script, you can "run" those by just importing it... – Torxed Jan 04 '17 at 23:19
  • Most likely, `sys.executable` isn't pointing to `C:\\Python27\\python.exe`. At least swap out `sys.executable` to eliminate a problem source, because you're chaining a lot of things here and any problem source you can take away from the equation will help you out greatly. You're also using `print "I'm alive"` which is a nifty debug tool, add more of those and see where your code breaks ;) – Torxed Jan 04 '17 at 23:22
  • thanks for the suggestions. I escaped everything and also changed the subprocess.call to C:\\Python27\\python.exe. but still no luck. the reason for calling the 3rd script is this set of tests are written so they acts the same as my main program (which has 3 tiers of python scripts. each script is waiting for some database variables to be set which can take 10-15 mins, thus the need to run the scripts without waiting for them to finish. – sonicspeed17 Jan 04 '17 at 23:51
  • Just so we're both on the same Python plane, calling `subprocess` on anything will not run the next script until that call is complete. Meaning `need to run the scripts without waiting for them to finish` won't hold up here. Unless you're talking about the PHP execution, that however will still also wait for the Python chain to complete.. If i'm not mistake, again, I'm not PHP guru. – Torxed Jan 04 '17 at 23:53
  • I'll bet that your `subprocess.call` is blocking your `python1.py` script - as I said - but you're expecting `python2.py` to start because you assumed `subprocess.call` is a non-blocking execution. Is this a correct assumption? – Torxed Jan 04 '17 at 23:59
  • I have some progress. the 2nd script IS started by the 1st script however it is terminated immediately before it can run any code. I set the 1st script to open the 2nd script with pop up a command line terminal (changed them from opening pythonw.exe to python.exe). when the 1st script is executed via PHP I get the 1st messagebox and when I clikc ok I see the command line window pop up momentarily. – sonicspeed17 Jan 05 '17 at 00:14

0 Answers0