开心姥姥 2019-11-12 05:25 采纳率: 50%
浏览 1766
已采纳

求助:Python的Subprocess.popen()返回的对象的stdin怎么用以下代码为何写入后没反应也不会报错

Python


class Thread(threading.Thread):
    def __init__(self, name, sub):
        threading.Thread.__init__(self)
        self.name = name
        self.sub = sub

    def run(self):
        while True:
            if self.sub.poll() is not None:
                break
            command = bytes(str(input("console:").encode('utf-8')), encoding="utf-8")
            # print(type(command))
                        #下面这句不会报错,也执行了,就是无效,跟没有这句一样为啥
            self.sub.stdin.write(command)
is_input_started = False
while True:
    empty = False
    try:
        enter = str(input('主文件路径:'))
    except SyntaxError:
        empty = True
    if not empty:
        if str(input('确定路径吗?(y,n):')) == 'y':
            break
sub = subprocess.Popen(enter, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
    while True:
        if sub.poll() is not None:
            print('Stopped')
            break
        out = sub.stdout.readline()
        if not out == "":
            print(out)
        if not is_input_started:
            thread = Thread.Thread('input', sub)
            thread.start()
            is_input_started = True

输入后并没有任何反应,怎么办
求助:Python的Subprocess.popen()返回的对象的stdin怎么用以下代码为何写入后没反应也不会报错
求助:Python的Subprocess.popen()返回的对象的stdin怎么用以下代码为何写入后没反应也不会报错
运行的程序是长时间一直运行
过程中需要互交于是这个问题就来了

  • 写回答

2条回答 默认 最新

  • lectre 2020-02-20 12:10
    关注

    windows系统上尝试stdin.write(command) 中得command字符串后添加'\r\n',随后调用一下stdin.flush()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记