如何通过进程ID获取进程的stdin?

tim*_*ger 9 python linux

我知道我可以像以下一样获得进程的stdin使用子进程python:

import subprocess
f = subprocess.Popen('python example.py',stdin=subprocess.PIPE)
f.stdin.write('some thing')
Run Code Online (Sandbox Code Playgroud)

但我只想知道我要写入流程的pid我该stdin 怎么做?

phi*_*hag 11

只需写信给/proc/PID/fd/1:

import os.path
pid = os.getpid() # Replace your PID here - writing to your own process is boring
with open(os.path.join('/proc', str(pid), 'fd', '1'), 'a') as stdin:
  stdin.write('Hello there\n')
Run Code Online (Sandbox Code Playgroud)