我正在使用 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 神经网络脚本时也会挂起。
可能是什么原因 ?