python怎么查看multiprocessing下进程的状态

新手上路,请多包涵

my situation goes like this:

first, I opened a subprocess while keeping a main process, somehow like
subprocess = Process(target=func, name='MySubprocess')

when I printed subprocess before subprocess.start(), I got info showing like this: <Process(MySubprocess), initial>;
after subrpocess.start(), print subprocess and I got <Process(MySubprocess), started>; after subrpocess.join(), I got <Process(MySubprocess, stopped)>.

So I am wondering now how to extract initial, started or even stopped. I know it is possbiel to use Subprocess.is_alive() to confirm the alive status of sub process, but it will be good it I can extract a status like 'initial' or 'stopped', then I can use this to make judgement per my requirement.

thx for your time in reviewing this question and giving me answers.

阅读 2.1k
1 个回答

应该没有直接的方法,瞅了眼源码,知道怎么定义和获取了,见实现代码

如果你觉得根据这个实现改写起来稍微有一点点麻烦,你还可以用一种比较简单粗暴的办法:

def process_state(p: Process) -> str:
    return str(p)[:-1].split(' ')[-1] # 和上面的结果稍微有点不同
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题