简体   繁体   English

PHP实时显示Python

[英]PHP display Python in real-time

I'm trying to get a simple Python script to output to PHP in real-time, and I find myself having trouble to accomplish it. 我试图获得一个简单的Python脚本以实时输出到PHP,但我发现自己很难做到这一点。

The script is nothing fancy, for testing purposes, just this: 该脚本没什么花哨的,出于测试目的,只是这样:

#!/usr/bin/python
import time

time.sleep(1)
print "message 1"
time.sleep(1)
print "message 2"
time.sleep(1)
print "message 3"
time.sleep(1)
print "message 4"

I've tried calling the Python code several ways. 我尝试了几种方法来调用Python代码。 It seems when I try to run it from: 看来,当我尝试从以下位置运行它时:

<?php
$output=shell_exec("/var/www/html/output.py");
print_r($output);
?>

It seems to wait for 4 secondes, and outputs the entire thing. 似乎要等待4秒钟,然后输出整个内容。

I've tried messing around with the ob_flush and flush() PHP commands, but I'm not able to get the effect that I want, it's essentually the same as the shell_exec() 我试过弄乱了ob_flush和flush()PHP命令,但是我无法获得想要的效果,它实际上与shell_exec()相同

And input would be greatly appreciated! 和输入将不胜感激!

You should probaby use the passthru() function for what you want. 您应该根据需要使用passthru()函数。

This is already answered in this SO Answer by Niklas Lindblad: https://stackoverflow.com/a/19736525/488191 这已在Niklas Lindblad的《 SO解答》中得到解答: https : //stackoverflow.com/a/19736525/488191

I recommend using passthru and handling the output buffer directly: 我建议使用passthru并直接处理输出缓冲区:

 ob_start(); passthru('/usr/bin/python2.7 /srv/http/assets/py/switch.py arg1 arg2'); $output = ob_get_clean(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM