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

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

问题描述

我正在寻找从外部进程将数据写入现有进程的 STDIN 的方法,并发现了类似的问题 如何将数据从不同的本地/远程进程流式传输到程序的 STDINPython?在stackoverlow中.

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(或其他文件描述符),即调用行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.

如果标准输入最初是一个管道,它就会工作.
例如,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天全站免登陆