如何从外部流程将数据写入现有流程的STDIN? [英] How to write data to existing process's STDIN from external process?

查看:111
本文介绍了如何从外部流程将数据写入现有流程的STDIN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找从外部流程将数据写入现有流程STDIN的方法,并且发现了类似的问题

I'm seeking for ways to write data to the existing process's STDIN from external processes, and found similar question How do you stream data into the STDIN of a program from different local/remote processes in Python? in stackoverlow.

@Michael在该线程中说,我们可以在如下所示的路径中获取现有进程的文件描述符,并允许在Linux上将数据写入它们.

In that thread, @Michael says that we can get file descriptors of existing process in path like below, and permitted to write data into them on Linux.

/proc/$PID/fd/

因此,我创建了一个下面列出的简单脚本,以测试将数据从外部进程写入脚本的STDIN(和TTY).

So, I've created a simple script listed below to test writing data to the script's STDIN (and TTY) from external process.

#!/usr/bin/env python

import os, sys

def get_ttyname():
    for f in sys.stdin, sys.stdout, sys.stderr:
        if f.isatty():
            return os.ttyname(f.fileno())
    return None

if __name__ == "__main__":
    print("Try commands below")

    print("$ echo 'foobar' > {0}".format(get_ttyname()))
    print("$ echo 'foobar' > /proc/{0}/fd/0".format(os.getpid()))

    print("read :: [" + sys.stdin.readline() + "]")

此测试脚本显示STDINTTY的路径,然后等待写入STDIN的路径.

This test script shows paths of STDIN and TTY and then, wait for one to write it's STDIN.

我启动了此脚本,并在下面收到了消息.

I launched this script and got messages below.

Try commands below
$ echo 'foobar' > /dev/pts/6
$ echo 'foobar' > /proc/3308/fd/0

因此,我从其他终端执行了命令echo 'foobar' > /dev/pts/6echo 'foobar' > /proc/3308/fd/0.执行完这两个命令后,在正在运行测试脚本的终端上两次显示消息foobar,仅此而已. print("read :: [" + sys.stdin.readline() + "]")行未执行.

So, I executed the command echo 'foobar' > /dev/pts/6 and echo 'foobar' > /proc/3308/fd/0 from other terminal. After execution of both commands, message foobar is displayed twice on the terminal the test script is running on, but that's all. The line print("read :: [" + sys.stdin.readline() + "]") was not executed.

是否有任何方法可以将来自外部进程的数据写入现有进程的STDIN(或其他文件描述符),即从其他进程中调用line print("read :: [" + sys.stdin.readline() + "]")的执行?

Are there any ways to write data from external processes to the existing process's STDIN (or other file descriptors), i.e. invoke execution of the lineprint("read :: [" + sys.stdin.readline() + "]") from other processes?

推荐答案

您的代码将不起作用.
/proc/pid/fd/0/dev/pts/6文件的链接.

Your code will not work.
/proc/pid/fd/0 is a link to the /dev/pts/6 file.

$ echo'foobar'>/dev/pts/6
$ echo'foobar'>/proc/pid/fd/0

$ echo 'foobar' > /dev/pts/6
$ echo 'foobar' > /proc/pid/fd/0

由于两个命令均写入终端.此输入将转到终端,而不是进程.

Since both the commands write to the terminal. This input goes to terminal and not to the process.

如果stdin最初是管道,它将起作用.
例如,test.py是:

It will work if stdin intially is a pipe.
For example, test.py is :

#!/usr/bin/python

import os, sys
if __name__ == "__main__":
    print("Try commands below")
    print("$ echo 'foobar' > /proc/{0}/fd/0".format(os.getpid()))
    while True:
        print("read :: [" + sys.stdin.readline() + "]")
        pass

运行方式为:

$ (while [ 1 ]; do sleep 1; done) | python test.py

现在从另一个终端将内容写入/proc/pid/fd/0,它将到达test.py

Now from another terminal write something to /proc/pid/fd/0 and it will come to test.py

这篇关于如何从外部流程将数据写入现有流程的STDIN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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