Python subprocess running in background before returning output

前端 未结 2 1908
终归单人心
终归单人心 2021-01-12 18:01

I have some Python code that I want to debug with perf. For that purpose I want to use subprocess. The following command returns instruction-related information of a process

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-12 18:32

    Use subprocess.Popen to run perf. Then, use pipe.communicate() to send input and get the process's output.

    After you've done, call pipe.terminate() to terminate the process.

    For example:

    pipe = subprocess.Popen(["perf","stat","-p",str(GetMyProcessID())], stdout=PIPE)
    
    pipe.terminate()
    stdout, stderr = pipe.communicate()
    print stdout
    

提交回复
热议问题