getting ProcessId within Python code

后端 未结 4 1008
渐次进展
渐次进展 2021-01-01 16:19

I am in Windows and Suppose I have a main python code that calls python interpreter in command line to execute another python script ,say test.py .

So test.py is exe

4条回答
  •  借酒劲吻你
    2021-01-01 17:06

    If you used subprocess to spawn the shell, you can find the process ID in the pid property:

    sp = subprocess.Popen(['python', 'script.py'])
    print('PID is ' + str(sp.pid))
    

    If you used multiprocessing, use its pid property:

    p = multiprocessing.Process()
    p.start()
    # Some time later ...
    print('PID is ' + str(p.pid))
    

提交回复
热议问题