2012-09-07 4 views
7

나는 내가 좋아하는 python에서 프로세스의 stdin 사용 서브 프로세스를 얻을 수 있습니다 알고프로세스 ID로 프로세스의 표준 입력을 가져 오는 방법은 무엇입니까?

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

하지만 난 단지 내가 쓰기 할 PID를 알고 원하는 프로세스의 stdin 내가 어떻게 할 수 있습니까?

+3

을 당신은 다른 프로세스의 표준 입력을 납치하고 싶어? 그건 아주 좋은 일이 아니에요 ... –

+0

예, 다른 프로세스의 표준 입력란에 – 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') 
관련 문제