我在使用PHP exec()函数时遇到了一些麻烦。每当我试图运行的脚本很短时,exec()就可以正常工作。但是如果脚本花费的时间超过一秒钟,它就会失败。请注意,我试图在命令行上手动运行长脚本,并且它工作得很好。看起来好像PHP解释器正在杀死我的外部脚本,如果它运行时间超过一秒钟左右。任何想法或建议?这里是我的代码:PHP exec()函数只运行极短的Python脚本
<?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")
附加说明:我已经增加最长执行时间限制在php.ini
,但我不认为应该的问题,因为“长脚本”只需要几秒钟就可以运行。另外,当我提到“短脚本”和“长脚本”时,我实际上是指同一个脚本。 “长脚本”和“短脚本”之间的区别仅仅是执行的时间,因为它可能会因文件的大小而异,我要求脚本处理。无论如何...任何建议将真正感激!
如果在命令行中键入python命令,会发生什么情况?提示是否立即返回?还是必须等待python脚本完成才能键入其他命令? –
如果我在命令行上运行python脚本,它工作得很好。 – crispytx
是的,但是当你从命令提示符运行时 - 一旦你输入命令后点击'enter',它会立即返回还是需要等到长时间运行的python进程完成? –