是否可以与Windows上的子流程3标准流进行交互?

问题描述

我有一个打开子流程的脚本,该流程等待多个输入。

我尝试使用subprocess.Popen(),但是当组合3个标准流时,它陷入了死锁...

所以我厌倦了使用wexpect(显然是pexecpt的Windows版本)。

现在我正在使用Sarge,现在我的代码如下:

import os
import subprocess
import sarge
import sys


def get_env():
    env = os.environ.copy()
    return env

def interactive(list_command: list,instruction_dict: dict):
    env = get_env()

    std_out = ""
    for a_number in instruction_dict:
        capture_stdout = sarge.Capture(buffer_size=-1)
        capture_stderr = sarge.Capture(buffer_size=-1)
        child_proc = sarge.Command(list_command,stdout=capture_stdout,stderr=capture_stderr,shell=True,env=env)
        child_proc.run(input=subprocess.PIPE,async_=True)
        print(capture_stdout.readlines(timeout=1.0),capture_stderr.readlines(timeout=1.0))
        if instruction_dict[a_number][0] is not None:
            #if capture_stdout.expect(instruction_dict[a_number][0]) is not None or capture_stderr.expect(instruction_dict[a_number][0]) is not None:
            #    self.test_manager.log_event(msg="found line : " + instruction_dict[a_number][0] + "in PIPE",current_event_type=LogLevel.DEBUG,screen_only=False)
            print("in exec line : ",child_proc.stdout.readline(timeout=1.0),child_proc.stderr.readlines(timeout=1.0))
        else:
            print("in exec line : ",child_proc.stdout.readlines(timeout=1.0),child_proc.stderr.readlines(timeout=1.0))
        if instruction_dict[a_number][1] is not None:
            print(f"sending \"{instruction_dict[a_number][1]}\" to process in interactive")
            temp_bytes = str.encode((instruction_dict[a_number][1]+"\n"))
            child_proc.stdin.write(temp_bytes)
            try:
                child_proc.stdin.flush() # blind flush
            except:
                pass

        print("Last line : ",child_proc.stderr.readlines(timeout=1.0))
        child_proc.wait()
        std_out += child_proc.stdout.read().decode('utf-8')
        child_proc.kill()    # kill the subprocess
        print("output: ",std_out)

interactive(list_command=["process.exe","-delete_datamodel","MODELNAME"],instruction_dict={1 : (None,"y")})

输出看起来像这样:

[] []
in exec line :  [] []
send "y" to process in interactive
Last line :  [] [b'User root connected to DB MODELNAME\r\n']
output:  Are you sure to delete DB MODELNAME? (y/n)\n\nDelete Data Model Ok\n

在控制台中,它看起来像这样:

C:\>process.exe -delete_data_model -db_name MODELNAME
Are you sure to delete DB MODELNAME ? (y/n)
y
User root connected to DB MODELNAME
Delete Data Model Ok

存在多个问题:

  • 在循环过程中,我无法捕获第一个标准输出“确定要删除DB MODELNAME吗?(y / n)”。我只能得到[]。

  • capture_stdout.expect崩溃,但实际上,如果它无法读取第一个标准输出,则可能与第一个相同。

  • 行使用stderr而不是stdout(这可能是因为该过程,我不知道如何测试)

  • 对于另一条命令,我将需要通过查看问题来回答多个互动问题,然后再以我的struction_dict如下所示的方式回答问题

instruction_dict = {1 : ("Are you sure to recover the DB","y"),2 : ("Do you want to stop the service",3 : ("Do you want to stop the DB",4 : ("Do you want to drop and create the DB","y")}

这甚至可能吗?我正在搜索一个工作示例,但没有找到一个示例,我知道我可能不好搜索,但是...

抱歉,语法很糟糕(英语不好),我想知道是否有一个纯python解决方案(我在tcl中有一个解决方案,但是我想摆脱它/我没有写过tcl代码)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...