程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了发送到python3程序的stdin大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决发送到python3程序的stdin?

开发过程中遇到发送到python3程序的stdin的问题如何解决?下面主要结合日常开发的经验,给出你关于发送到python3程序的stdin的解决方法建议,希望对你解决发送到python3程序的stdin有所启发或帮助;

你所需要的是一些符合主.py):

from subprocess import Popen, PIPE, STDOUT
x = Popen(['some_child.exe', 'parameter'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)

while x.poll() is None:
    child_output = x.stdout.readline()
    print(child_output)
    if b'Do you want to send the argument?' in child_output:
        x.stdin.write(b'yes\n')
        x.stdin.flush()
x.stdout.close()
x.stdin.close()

You’re assuming child.exe (in your mockup demo, python.exe) is communicaTing with @H_476_3@main.py via sys.stdin/stdout, however these I/O’s are used to communicate with the sHell that spawned the process.

很像childs的“stdout/stdin”将与 产生了这个过程,在本例中是“Popen()”。 每个派生的子进程子流程.Popen(…)将与 它有自己的“stdout/stdin/stderr”,否则每个子进程都会产生巨大的 你的主要进程的混乱。这意味着你必须检查 在该特定子进程上输出并相应地写入,如中所做 上面的例子。 一种看待它的方式是:![输入图像描述] [这里](https://i.stack.imgur.com/L2QWa.png) 你要开始了主.py,并通过系统标准输出以及标准输入. 中的每个input()``主.py将输出某些内容到系统标准输出 这样你就可以读了。 同样的逻辑也适用于子.exewhere everyinput()将 输出一些东西到它的系统标准输出但请记住-sys不是共享的 过程间的变量)。

import sys

if sys.argv[1] == 'start':
    inp = input('Do you want to send the argument?\n').lower()
    if inp == 'no':
        sys.exit()
    elif inp == 'yes':
        #Somehow send '1' to the stdin of 1.py while it is running
        sys.stdout.write('1')
        sys.stdout.flush()

但是一个简单的’print(1)也会这样做,因为它基本上是输出的 “1”到系统标准输出`为了你。 编辑2018:不要忘记关闭输入和输出,因为它们可能会离开 打开文件系统描述符 晚年的问题。

其他信息传递者

假设您控制了要子.exe你可以修改 无论如何,其他一些选项包括:套接字—使用常规套接字进行通信,在nix上,最有效的是Unix套接字。

更小心的尾巴!

.readline()将假定数据中的某个地方有一个\n,很可能在末尾。我切换到.readline()有两个原因,.read()将挂起并等待’EOF’,除非您指定要读取的字节数(如果我不骑自行车的话)。为了能够阅读你需要合并的各种输出选择。选择()在你的代码中-或者在某种类型的缓冲区中,你调用x。标准读取(1)一次读一个字节。因为如果您尝试读取.read(1024)并且缓冲区中没有1024个字节,那么您的读取将挂起,直到有1024个字符为止。我在你的房间里留了个虫子儿童.py专门编写代码(我的作品)-这是一个简单的基本Python-希望这是一个如何调试错误的学习经验(你提到你不擅长它,这是一个学习的方法)。

解决方法

我得去查档案主.py以及儿童.py.
我正试图把一个字符串发送到主.py.
这是我不完整的代码:

主.py

from subprocess import *
import time

def main():
    program = Popen(['python.exe'. 'child.py','start'])
    while True: #waiTing for'1' to be sent to the stdin
        if sys.stdin == '1':
            print('text)

if __name__ == '__main__':
    main()

child.py

import sys

if sys.argv[1] == 'start':
    inp = input('Do you want to send the argument?\n').lower()
    if inp == 'no':
        sys.exit()
    elif inp == 'yes':
        #Somehow send '1' to the stdin of 1.py while it is running

I have no idea how to do this.

I am running windows 10 with python 3.5.1

-谢谢
编辑:当我将参数发送回主.py,我无法重新打开
程序。操作系统重新打开在我的情况下没有用的程序。
这些程序是我尝试做的一个小演示。在我的实际生活中
程序,我不能这样做,因为这两个程序是“沟通”的
彼此都需要时刻敞开心扉。
我需要的是一种方法,把一个论点主.py也许是用
但是当我发送我的参数时,它不能重新打开程序。一些
例如操作系统重新打开程序,这不是我想要的
去吧。我需要主.py随时打开。
我有我的新的当前代码,这是不工作。一个窗口弹出然后
关闭。

@H_191_92@main.py
from subprocess import Popen,PIPE,STDOUT
x = Popen(['python.exe','2.py','start'],stdout=PIPE,stdin=PIPE,stderr=STDOUT)
while x.poll() is None:
    if b'Do you want to send the argument?' in x.stdout.read():
        x.stdin.write(b'yes\n')

child.py

import sys
import time
time.sleep(1)
if 1 = 1:
    inp = input('Do you want to send the argument?\n').lower()
    if inp == 'no':
        sys.exit()
    elif inp == 'yes':
        sys.stdout.write('1')
        sys.stdout.flush()

That is my code.

大佬总结

以上是大佬教程为你收集整理的发送到python3程序的stdin全部内容,希望文章能够帮你解决发送到python3程序的stdin所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。