0

我正在使用 PHP 在浏览器中显示 Python 输出。由于 Python 脚本需要很长时间才能打印输出,因此用户界面会挂起,直到 Python 脚本完成执行。

在 PHP 中启用了 Pthreads for Windows 以对调用 Python 脚本的shell_exec()函数进行多线程处理。尽管使用了 Pthreads,用户界面仍然挂起。

这是代码:

$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); 

即使使用多线程,用户界面(由 PHP 生成)在调用 Python 神经网络脚本时也会挂起。

可能是什么原因 ?

4

1 回答 1

0

PHP 正在缓存输出。您需要停用输出缓存。然后您的浏览器将不会挂起。

见 ob_flush()

于 2019-12-22T06:35:56.217 回答