python 3 中的os.getpid() 与 os.getpgid()

今天调试代码遇到一个小问题,代码如下:

def worker(msg):
    t_start = time.time()
    print("%s start process, this id is: %d" % (msg, os.getpgid()))
    time.sleep(random.random() * 2)
    t_stop = time.time()
    print(msg, "finish, this time is %d0,2f" % (t_stop - t_start))

本意是查看系统的pid进程os.getpid(),不小心写成了os.getpgid(),报错“AttributeError: module 'os' has no attribute 'getpgid'”。

找了好一会没看出原因,最后翻看函数表才发现问题,为自己的粗心留个贴。时刻告诫自己小心谨慎。

os.getpgid(pid)

返回进程ID为pid的进程的组ID,如果pid=0,则返回当前调用进程的进程组ID

os.getpid()

返回当前进程的进程ID

os.getppid()

返回当前进程的父进程的ID

os.getsid(pid)

返回进程ID为pid的进程所在的会话的会话ID