gpt4 book ai didi

php - 如何让 PHP 停止自动截断输出

转载 作者:太空宇宙 更新时间:2023-11-03 17:07:07 25 4
gpt4 key购买 nike

我有一个 PHP 脚本,它调用一个 Python 脚本,该脚本循环遍历一个文本文件,将特定行输出到网页。 PHP 脚本调用 Python 脚本并回显其输出,如下所示...

$output = shell_exec($command);
echo ($output);

相关的Python代码是这样的...

for myStr in myList:
myObj=re.match(r'(\s|\S)+\[(?P<specificVal>\d+)(\s|\S)+',myStr)
specificVal = int(myObj.group('specificVal'))
if specificVal in range(min,max):
print ('<p>%s</p>' % (myStr))

这适用于大约一百行,然后停止输出,当可能有几千行时。有没有更好的方法来做到这一点,或者我只需要编辑一些 php.ini 选项?谢谢!

最佳答案

这可能与 shell_exec() 由于某种原因未返回所有输出有关。您可以尝试使用更强大的 proc_open() :

<?php

$fileDescriptors = array(
0 => array("pipe", "r"), // STDIN
1 => array("pipe", "w"), // STDOUT
2 => array("pipe", "w") // STDERR
);
$pipes = array();
flush();

$process = proc_open($command, $fileDescriptors, $pipes, realpath('.'), array());
if (!is_resource($process))
{
error_log("Failed to execute '$command'");
return;
}

while ($data = fgets($pipes[1]))
{
print $data;
flush();
}

如果不在每次循环迭代中调用 flush();,您可能会获得一些性能优势。

如果这仍然没有给您提供所有输出,我会怀疑 Python 脚本中存在问题,您应该发布更多内容以使其更易于调试。

关于php - 如何让 PHP 停止自动截断输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34485998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com