2

I'm trying to run a Python script from PHP on a webserver. The script only contains one line

print "Hello, World"

and if I run it from the terminal, it takes 0.028 seconds. When I try doing this from PHP though

exec('python python_test.py');

it takes 125 seconds (but it does execute the script in the end). I read that there is an overhead because a new shell instance needs to be created whenever exec is called from php (PHP exec() performance), but that's not the problem since when I ran:

exec('echo hi');

it only took 0.013 seconds. So it's really the Python script that is running very slow. Any thoughts on how to make it faster?

I also tried the PHP commands shell_exec() and system() but they don't improve the speed.

4
  • How fast does your python script run when invoked by itself on the command line (without going through PHP)? If it turns out it's slow then we'll need to see the python script to find the bottleneck (or you could file up a profiler to find it yourself). Commented Feb 3, 2014 at 12:52
  • @webbiedave: that's answered in the question. Commented Feb 3, 2014 at 12:54
  • It's the third line. 0.028 seconds. And the second line is the python script. All of it. Commented Feb 3, 2014 at 12:56
  • Oh. You're right. I missed that. Commented Feb 3, 2014 at 12:57

0

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.