Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP exec()函数只运行极短的Python脚本_Php_Python_Python 3.x_Php 7 - Fatal编程技术网

PHP exec()函数只运行极短的Python脚本

PHP exec()函数只运行极短的Python脚本,php,python,python-3.x,php-7,Php,Python,Python 3.x,Php 7,我在使用PHP exec()函数时遇到一些问题。每当我试图运行的脚本很短时,exec()就可以正常工作。但是如果脚本花费的时间超过一秒钟左右,它就会失败。注意,我曾尝试在命令行上手动运行长脚本,但效果很好。如果PHP解释器运行的时间超过一秒左右,它似乎会杀死我的外部脚本。有什么想法或建议吗?这是我的密码: <?php $fileName = "foobar.docx"; $argVar = $fileName; exec("python3 /var/www/html

我在使用PHP exec()函数时遇到一些问题。每当我试图运行的脚本很短时,exec()就可以正常工作。但是如果脚本花费的时间超过一秒钟左右,它就会失败。注意,我曾尝试在命令行上手动运行长脚本,但效果很好。如果PHP解释器运行的时间超过一秒左右,它似乎会杀死我的外部脚本。有什么想法或建议吗?这是我的密码:

<?php
    $fileName = "foobar.docx";
    $argVar = $fileName;
    exec("python3 /var/www/html/jan8/alexandrina.py /var/www/html/jan8/$argVar");
    echo "$output";
?>

这是我的剧本:

#!/usr/bin/env python3

import docx
import sys

docxFile = "".join(sys.argv[1:])

# The Three Lines Below Create a New Variable "htmlFile"
# "htmlFile" is the same as "docxFile", except ".docx" is cut off
# and replaced with ".html"
myNumber = len(docxFile) - 5
htmlFile = docxFile[0:myNumber]
htmlFile = htmlFile + '.html'

def generateHTML(filename):
    doc = docx.Document(filename)
    fullText = []
    for para in doc.paragraphs:
        fullText.append('<p>')
        fullText.append(para.text)
        fullText.append('</p>')
        fullText.append('\n')
    return '\n'.join(fullText)

file = open(htmlFile, "w")
file.write(generateHTML(docxFile))
file.close()

print("end python script")
#/usr/bin/env蟒蛇3
进口docx
导入系统
docxFile=”“.join(sys.argv[1:])
#下面的三行创建一个新变量“htmlFile”
#“htmlFile”与“docxFile”相同,只是“.docx”被截断
#并替换为“.html”
myNumber=len(docxFile)-5
htmlFile=docxFile[0:myNumber]
htmlFile=htmlFile+'.html'
def generateHTML(文件名):
doc=docx.Document(文件名)
全文=[]
对于文件段落中的段落:
全文。追加(“”)
全文。追加(段落文本)
全文。追加(“

”) 全文。追加('\n') 返回'\n'。加入(全文) 文件=打开(htmlFile,“w”) file.write(generateHTML(docxFile)) file.close()文件 打印(“结束python脚本”)

附加说明:我在
php.ini
中增加了最大执行时间限制,但我认为这并不重要,因为“长脚本”只需几秒钟即可运行。另外,当我提到“短脚本”和“长脚本”时,我实际上指的是同一个脚本。“长脚本”和“短脚本”之间的区别只是执行的时间,因为它可能根据我请求脚本处理的文件的大小而有所不同。无论如何任何建议都将不胜感激

通常,php exec函数应该阻塞,直到您运行的命令完成。也就是说,PHP脚本将停止,等待命令完成,直到继续使用脚本的其余部分。我有一半的想法是您的服务器正在经历一个max_execution_time超时,但是您已经明确指出,即使是几秒钟也太长了,即使是这些相当短的脚本也有问题

我想到了几个解决办法。最简单的方法是修改python命令,以便a)将任何输出路由到文件或输出流,b)在后台运行进程。根据报告:

如果使用此函数启动程序,为了使其在后台继续运行,程序的输出必须重定向到文件或其他输出流。否则将导致PHP挂起,直到程序执行结束

我还希望您为exec函数使用另外两个可选参数

$fileName = "foobar.docx";
$argVar = $fileName;
$cmd = "python3 /var/www/html/jan8/alexandrina.py /var/www/html/jan8/$argVar";
// modify your command to toss output, background the process, and output the process id
$cmd_modified = $cmd . " >/dev/null & echo \$!";
$cmd_output = NULL; // this will be an array of output
$cmd_return_value = NULL; // this will be the return value of the script
exec($cmd_modified, $cmd_output, $cmd_return_value);
echo "exec has completed</br>";
echo "output:<br>" . print_r($cmd_output, TRUE) . "<br>";
echo "return value: " . print_r($cmd_return_value, TRUE);
对此

$cmd_modified = $cmd . " & echo \$!";

让我们知道$cmd_output的输出是什么——它至少应该有新生成的进程的进程id。

感谢S.Imp提供的所有帮助。我在使用您的建议进行调试时遇到了一些问题,因为我碰巧使用AJAX调用脚本。但是,我使用您的建议编写了更简单的脚本来尝试调试问题,我发现:

数组([0]=>Traceback(最近一次调用):[1]=>File”/var/www/html/jan8/alexandrina.py”,第28行,在[2]=>File.write(generateHTML(docxFile))[3]=>unicodeincoder错误:“ascii”编解码器无法对位置25处的字符“\u2026”进行编码:序号不在范围内(128))

因此,问题似乎与ascii编码有关!尽管较大的文件只是一个docx文件,与较短的docx文件具有相同的文本,但它重复了300页。似乎如果一个docx文件超过1页,就会插入单页docx文件中不存在的ascii字符。我不知道这篇文章是否会帮助任何人,但谁知道呢


[已解决]

如果在命令行中键入python命令,会发生什么?提示是否立即返回?或者您必须等待python脚本完成后才能键入另一个命令?如果我在命令行上运行python脚本,它可以正常工作。是的,但是当您在命令提示符下运行时--在键入命令后单击“enter”后,它是立即返回还是必须等待长时间运行的python进程完成?当我运行脚本处理一个大文件时,需要几秒钟才能完成进程,它不会立即返回。但是,如果我运行脚本来处理一个小文件,它几乎会立即返回。非常感谢您对S.Imp的帮助。我最终能够使用您的调试建议解决问题。非常感谢你的帮助!如果没有你的帮助,可能无法解决问题。很高兴你解决了问题。如果使用exec,则应始终注意返回值。
$cmd_modified = $cmd . " & echo \$!";