2012-09-07 63 views
7

我知道我可以在python像獲取進程的stdin使用子:如何通過進程ID獲取進程的stdin?

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

,但我只想知道我想寫的PID進程的stdin 我怎麼能做到這一點?

+3

你想劫持另一個進程的標準輸入?這不是一件很好的事情... –

+0

是的,我想寫一些someting到其他進程的標準輸入 – timger

+0

你想要什麼機器? – 0x90

回答

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')