0

I'm using PHP to display the Python output in the browser. Since the Python script takes a long time to print the output, The User Interface gets hung till the Python script finishes its execution.

Pthreads for Windows is enabled in PHP for multithreading the shell_exec( ) function which invokes the Python script. Inspite of using Pthreads, the User Interface still gets hung.

Here is the code:

$output_customer_churn_prediction = "";

class WorkerThreads extends Thread {
    private $workerId;

    public function __construct($id)
    {
        $this->workerId = $id;
    }

    public function run()
    {
        sleep(rand(0, 3));
        echo "Worker {$this->workerId} ran" . PHP_EOL;
        $output_customer_churn_prediction = shell_exec('python '.$_SERVER['DOCUMENT_ROOT'].'/analytics/python/neural-net-prediction-customer-attrition.py');
        print_r($output_customer_churn_prediction);
    }
}

$workers[0] = new WorkerThreads(0);
$workers[0]->start();

print_r($output_customer_churn_prediction, true); 

Even with multithreading, the User Interface (generated by PHP) gets hung while invoking the Python Neural Net script.

What could be the reason ?

User456898
  • 5,704
  • 5
  • 21
  • 37
  • 1
    **`wwwuser > PHP > Shell > Python > Shell > PHP > wwwuser`** you got a `xeon x32 cpu` ? Why don't use as `CGI` script ? (`wwwuser` is httpd) – dsgdfg Sep 21 '16 at 07:32
  • Or why don't use as `python service` ? (without apache, etc.) – dsgdfg Sep 21 '16 at 07:35
  • 1
    If I understood you correctly, you're executing the script to serve a web request. Threading will not in any way help you in that case, unless you're running multiple scripts in a request (it might not help you then either). From reading the [php documentation for threads](http://php.net/manual/en/class.thread.php) it seems that PHP does some magic implicit thread joining. That means that your request has to wait for your thread to finish before it can respond. It'd have to wait anyway, as you want to display the results. – Ilja Everilä Sep 21 '16 at 07:36

1 Answers1

0

PHP is caching the Output. You Need to deactivate the Output cache. Then your Browser will not hang.

See ob_flush()