Python得到进程信息

Date: 2019/10/27 Tags: Python


import psutil
import threading

def get_index_in_proc_group():
    pid = os.getpid()
    ppid = os.getppid()
    pids = []
    for p in  psutil.process_iter():
        try:
            if p.ppid() == ppid:
                pids.append(p.pid)
        except:
            pass
    pids = sorted(pids)
    return pids.index(pid)


def get_thread_index():
    return [t for t in threading.enumerate()\
        if 'Dask-Worker-Threads' in str(t)].index(threading.current_thread())

def get_self_index():
    return (get_index_in_proc_group(), get_thread_index(),)