您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Windows上具有恒定输出的Python无块子过程输入

Windows上具有恒定输出的Python无块子过程输入

proc.communicate()等待子进程完成,因此最多只能使用 一次 –您可以 一次 传递 所有 输入,并在子进程退出获取所有输出

如果不修改输入/输出,则无需重定向子流程的stdin / stdout。

要将输入馈送到后台线程中的子流程,并在一行一行到达时立即打印其输出

#!/usr/bin/env python3
import errno
from io import TextIOWrapper
from subprocess import Popen, PIPE
from threading import Thread

def Feed(pipe):
    while True:
        try: # get input
            line = input('Enter input for minecraft')
        except EOFError:
            break # no more input
        else:
            # ... do something with `line` here

            # Feed input to pipe
            try:
                print(line, file=pipe)
            except BrokenPipeError:
                break # can't write to pipe anymore
            except OSError as e:
                if e.errno == errno.EINVAL:
                    break  # same as EPIPE on Windows
                else:
                    raise # allow the error to propagate

    try:
        pipe.close() # inform subprocess -- no more input
    except OSError:
        pass # ignore

with Popen(["java", "-jar", "minecraft_server.jar"],
           cwd=r'C:\Users\Derek\Desktop\server',
           stdin=PIPE, stdout=PIPE, bufsize=1) as p, \
     TextIOWrapper(p.stdin, encoding='utf-8', 
                   write_through=True, line_buffering=True) as text_input:
    Thread(target=Feed, args=[text_input], daemon=True).start()
    for line in TextIOWrapper(p.stdout, encoding='utf-8'):
        # ... do something with `line` here
        print(line, end='')

注意事项p.stdin

我的世界的输出可能会延迟到刷新其标准输出缓冲区。

如果您在 _“line在此处执行某些操作”_注释周围没有可添加内容,请不要重定向相应的管道(暂时忽略字符编码问题)。

TextIOWrapper认情况下使用通用换行模式。newline如果不想,请明确指定参数。

python 2022/1/1 18:28:28 有188人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶