子进程 wait() 函数似乎没有等待子进程完成
我正在尝试使用 python 的子进程模块运行 php 脚本。
proc = subprocess.Popen(['php', '-f', test.php], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
retCode = proc.wait
print retCode
val = float(kprocess.stdout.read())
return val
我也尝试过:
proc = subprocess.Popen(['php', '-f', test.php], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
val = float(kprocess.communicate()[0])
return val
当我在 python 解释器中运行它时,这两种方法都可以在本地工作,但是当我尝试在实际服务器上运行它时,我总是得到“ValueError at/empty string for float()”。这让我相信这个过程在某种程度上并没有被等待。我缺少什么?
编辑:我正在使用 Django,所以当我使用 Django 运行时它似乎才会中断。
I'm trying to run a php script with python's subprocess module.
proc = subprocess.Popen(['php', '-f', test.php], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
retCode = proc.wait
print retCode
val = float(kprocess.stdout.read())
return val
I've also tried:
proc = subprocess.Popen(['php', '-f', test.php], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
val = float(kprocess.communicate()[0])
return val
Both ways work locally when I just run it in a python interpreter, however when I try to run it on the actual server, I always get "ValueError at / empty string for float()". This leads me to believe that the process is somehow not being waited for. What am I missing?
EDIT: I'm using Django, so it only seems to break when I run with Django.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须实际调用进程的
wait
< /a> 函数:但是,由于您要将输出重定向到,因此您应该注意
wait
文档中的警告并使用沟通
。确保您的代码没有语法错误:test.php
可能不是变量名称,而是字符串proc
和kprocess
communicate
的结果(严格来说这不是一个错误,但会阻碍错误检测和跟踪)相反,我建议:
You have to actually call the processes'
wait
function:However, since you are redirecting the output to, you should heed the warning in the
wait
docs and usecommunicate
instead. Make sure your code is free of syntactic errors:test.php
is probably not a variable name, but a stringproc
andkprocess
communicate
(This is not strictly an error, but can hinder error detection and tracing)Instead, I'd suggest: