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

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.


回答:

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

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

python3">def process_state(p: Process) -> str:

return str(p)[:-1].split(' ')[-1] # 和上面的结果稍微有点不同

以上是 python怎么查看multiprocessing下进程的状态 的全部内容, 来源链接: utcz.com/p/938210.html

回到顶部