如何通过进程ID获取进程的标准输入? [英] How can I get a process's stdin by a process id?

查看:279
本文介绍了如何通过进程ID获取进程的标准输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以在python中获取进程的stdin use子进程,例如:

I know i can get process's stdin use subprocess in python like:

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

但是我只想知道要写入进程stdin的pid 我该怎么办?

but I want only know the pid which i want to write to the process's stdin how can i do this?

推荐答案

只需写入/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')

这篇关于如何通过进程ID获取进程的标准输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆