使用子进程 wait() 和 poll()

发布于 2024-09-05 02:56:20 字数 304 浏览 1 评论 0原文

我正在尝试编写一个使用 subprocess 模块的小应用程序。

我的程序调用一个外部 Bash 命令,需要一些时间来处理。在此期间,我想向用户显示一系列如下消息:

正在处理。请稍等...
输出是 foo()

如何使用 Popen.wait()Popen.poll() 执行此操作。我读到我需要使用 Popen.returncode,但我不知道如何让它主动检查状态。

I am trying to write a small app that uses the subprocess module.

My program calls an external Bash command that takes some time to process. During this time, I would like to show the user a series of messages like this:

Processing. Please wait...
The output is foo()

How can I do this using Popen.wait() or Popen.poll(). I have read that I need to use the Popen.returncode, but how I can get it to actively check the state, I don't know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

始终不够爱げ你 2024-09-12 02:56:20

两者wait()< /a> (指定了 timeout)和 poll() 如果进程尚未完成,则返回 None;如果进程已经完成,则返回不同的内容(我认为一个整数,退出代码,希望为 0)。

编辑

wait()poll() 有不同的行为:

  • wait(没有超时参数)将阻塞并等待进程完成。
  • 带有 timeout 参数的 wait 将等待 timeout 秒以让进程完成。如果未完成,则会抛出 TimeoutExpired 异常。如果您捕获到异常,欢迎您继续,或者再次等待
  • poll 总是立即返回。它有效地执行超时为 0 的等待,捕获任何异常,并在进程尚未完成时返回 None
  • 使用 waitpoll,如果进程已完成,popen 对象的 returncode 将被设置(否则它是 None - 您可以像调用 waitpoll),函数的返回值也将是进程的返回码。

所以我认为你应该这样做:

while myprocess.poll() is None:
    print("Still working...")
    # sleep a while

请注意,如果 bash 脚本创建大量输出,你必须使用 communicate() 或类似的东西防止 stdoutstderr 被塞满。

Both wait() (with timeout specified) and poll() return None if the process has not yet finished, and something different if the process has finished (I think an integer, the exit code, hopefully 0).

Edit:

wait() and poll() have different behaviors:

  • wait (without the timeout argument) will block and wait for the process to complete.
  • wait with the timeout argument will wait timeout seconds for the process to complete. If it doesn't complete, it will throw the TimeoutExpired exception. If you catch the exception, you're then welcome to go on, or to wait again.
  • poll always returns immediately. It effectively does a wait with a timeout of 0, catches any exception, and returns None if the process hasn't completed.
  • With either wait or poll, if the process has completed, the popen object's returncode will be set (otherwise it's None - you can check for that as easily as calling wait or poll), and the return value from the function will also be the process's return code.

</Edit>

So I think you should do something like:

while myprocess.poll() is None:
    print("Still working...")
    # sleep a while

Be aware that if the bash script creates a lot of output you must use communicate() or something similar to prevent stdout or stderr to become stuffed.

我们只是彼此的过ke 2024-09-12 02:56:20

@extraneon 的答案有点倒退。如果进程已完成,wait()poll() 都会返回进程的退出代码。如果进程仍在运行,poll() 方法将返回 None,并且 wait() 方法将阻塞,直到进程退出:

查看以下页面: https://docs.python.org/3.4/library/subprocess.html# popen-objects

Popen.poll()

检查子进程是否已终止。设置并返回 returncode 属性。

Popen.wait()

等待子进程终止。设置并返回 returncode 属性。

@extraneon's answer is a little backwards. Both wait() and poll() return the process's exit code if the process has finished. The poll() method will return None if the process is still running and the wait() method will block until the process exits:

Check out the following page: https://docs.python.org/3.4/library/subprocess.html#popen-objects

Popen.poll()

Check if child process has terminated. Set and return returncode attribute.

Popen.wait()

Wait for child process to terminate. Set and return returncode attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文