首页 > 解决方案 > Is it a bad idea to run a python script and fetch its output via PHP?

问题描述

I am using php to run a python script and fetching its output using json.dump and showing on my php page. I feel its slower than when I run it through python idle.

标签: phppython

解决方案


If I understand your question correctly it's no surprise that it feels slower because calling your Python script from PHP, instead of calling it from CLI, increases the operations your PC has to execute. Consider this: first PHP has to create a shell to call your script, then wait for it to finish (e.g. wait for an exit code appearing in the buffer), grab everything from the buffer and then push it into the output buffer and then flush the output buffer, so the data is actually displayed on your page. And besides all of that your output data is transported twice, first from Python to PHP and then from PHP to your browser.

Furthermore, the processing speed depends on the method you use to call your Python script - there are a couple of ways to achieve this and some have more overhead than others.


推荐阅读