子进程 wait() 函数似乎没有等待子进程完成

发布于 2024-11-15 23:25:45 字数 621 浏览 9 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀里藏娇 2024-11-22 23:25:45

您必须实际调用进程的 wait< /a> 函数:

proc = subprocess.Popen(...)
retCode = proc.wait # retCode will be the function wait
retCode = proc.wait() # retCode will be the return code

但是,由于您要将输出重定向到,因此您应该注意 wait 文档中的警告并使用 沟通。确保您的代码没有语法错误:

  • test.php 可能不是变量名称,而是字符串
  • 您混淆了两个变量名称,prockprocess
  • 你盲目地解析了 communicate 的结果(严格来说这不是一个错误,但会阻碍错误检测和跟踪)

相反,我建议:

proc = subprocess.Popen(['php', '-f', 'test.php'],
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout,stderr = proc.communicate()
if proc.returncode != 0:
    raise Exception('Test error: ' + stderr)
return float(stdout)

You have to actually call the processes' wait function:

proc = subprocess.Popen(...)
retCode = proc.wait # retCode will be the function wait
retCode = proc.wait() # retCode will be the return code

However, since you are redirecting the output to, you should heed the warning in the wait docs and use communicate instead. Make sure your code is free of syntactic errors:

  • test.php is probably not a variable name, but a string
  • You're confusing two variable names, proc and kprocess
  • You're blindly parsing the result of communicate (This is not strictly an error, but can hinder error detection and tracing)

Instead, I'd suggest:

proc = subprocess.Popen(['php', '-f', 'test.php'],
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout,stderr = proc.communicate()
if proc.returncode != 0:
    raise Exception('Test error: ' + stderr)
return float(stdout)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文