Answer a question

I'm launching a number of subprocesses with subprocess.Popen in Python. I'd like to check whether one such process has completed. I've found two ways of checking the status of a subprocess, but both seem to force the process to complete. One is using process.communicate() and printing the returncode, as explained here: checking status of process with subprocess.Popen in Python. Another is simply calling process.wait() and checking that it returns 0.

Is there a way to check if a process is still running without waiting for it to complete if it is?

Answers

Ouestion: ... a way to check if a process is still running ...

You can do it for instance:

p = subprocess.Popen(...
"""
A None value indicates that the process hasn't terminated yet.
"""
poll = p.poll()
if poll is None:
  # p.subprocess is alive

Python » 3.6.1 Documentation popen-objects

Tested with Python:3.4.2

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐