getting ProcessId within Python code

后端 未结 4 1023
渐次进展
渐次进展 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 16:54

    It all depends on how you're launching the second process.

    If you're using os.system or similar, that call won't report back anything useful about the child process's pid. One option is to have your 2nd script communicate the result of os.getpid() back to the original process via stdin/stdout, or write it to a predetermined file location. Another alternative is to use the third-party psutil library to figure out which process it is.

    On the other hand, if you're using the subprocess module to launch the script, the resulting "popen" object has an attribute popen.pid which will give you the process id.

提交回复
热议问题