Nic*_*Nic 2 python stdin python-3.x
我有文件,main.py和child.py.
我正在尝试将字符串发送到main.py的stdin .
这是我不完整的代码:
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()
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么做.
我用python 3.5.1运行Windows 10
-谢谢
编辑:当我将参数发送回main.py时,我无法重新打开该程序.os.system重新打开程序,这在我的案例中没用.
这些程序是我想要做的一个小型演示.在我的实际程序中,我无法做到这一点,因为两个程序彼此"通信"需要始终打开.
我需要回答的是一种向main.py发送参数的方法,也许是使用stdin但是当我发送我的参数时,它无法重新打开程序.像os.system这样的例子重新打开程序,这不是我想要做的.我需要一直打开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')
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
那是我的代码.
你需要的是(in main.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()
Run Code Online (Sandbox Code Playgroud)
您假设child.exe(在您的模型演示中python.exe)正在与main.pyvia进行通信sys.stdin/stdout,但是这些I/O用于与产生进程的shell进行通信.
stdout/stdin在这种情况下,很像孩子们将与产生该过程的shell进行通信Popen().
每个生成的子进程都subprocess.Popen(...)将被它自己隔离stdout/stdin/stderr,否则每个子进程都会使你的主进程stdout/stdin大不相同.这意味着您必须检查该特定子进程的输出并相应地写入,如上例所示.
你正在开始main.py,并通过sys.stdout和你沟通sys.stdin.每个input()人main.py都会输出一些内容,sys.stdout以便您可以阅读.
完全相同的逻辑适用于child.exe每个input()人输出内容的地方sys.stdout ( - 但请记住 - 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()
Run Code Online (Sandbox Code Playgroud)
但一个简单的print(1)会做同样的,因为它基本上将输出1到sys.stdout你.
编辑2018:不要忘记关闭输入和输出,因为它们可能会在文件系统上留下打开的文件描述符,占用资源并在以后的生活中造成问题.
假设您可以控制代码,child.exe并且可以以任何方式修改通信管道,其他一些选项是:
.readline()将假设\n您的数据中存在某个位置,最有可能在最后.我切换到.readline()两个原因,.read()将挂起并等待,EOF除非你确切地指定要读取多少字节,如果我不骑自行车.为了能够读取所有类型的输出,您需要将select.select()合并到您的代码中 - 或者您调用一次x.stdout.read(1)读取一个字节的某种缓冲区.因为如果您尝试读取.read(1024)并且缓冲区中没有1024个字节,则读取将挂起,直到有1024个字符.
我child.py故意在你的代码中留下了一个错误(我的工作) - 这是微不足道的基本Python - 希望它是如何调试错误的学习经验(你提到你不擅长它,这是一种学习的方法) .
| 归档时间: |
|
| 查看次数: |
2970 次 |
| 最近记录: |