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

时间:2012-09-07 06:21:20

标签: python linux

我知道我可以在stdin中使用进程的python使用子进程,如:

import subprocess
f = subprocess.Popen('python example.py',stdin=subprocess.PIPE)
f.stdin.write('some thing')

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

1 个答案:

答案 0 :(得分: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')