简体   繁体   English

Python子进程等待按键

[英]Python subprocess wait keypress

I want to run a program using the subprocess module.我想使用 subprocess 模块运行程序。 While running the program, there are cases where it waits for a button press to continue.在运行程序时,在某些情况下它会等待按下按钮以继续。 It is not waiting for this input to the stdin input.它不等待此输入​​到 stdin 输入。 The input sent to stdin is ignored by the program.程序忽略发送到标准输入的输入。 Only when I press a button on the console will the program continue running.只有当我按下控制台上的按钮时,程序才会继续运行。

I tried it like this:我试过这样:

proc = Popen("festival.exe level.sok", stdin=PIPE, text=True)
proc.stdin.write('.')
proc.stdin.flush()

It this case nothing happened.这种情况下什么也没发生。

and like this:像这样:

proc = Popen...
proc.communicate('.')

In this case, the python code will not continue running until I press a button on the console.在这种情况下,python 代码将不会继续运行,直到我按下控制台上的按钮。 If I set a timeout then the python code continues to run but festival.exe does not continue to run.如果我设置了超时,那么 python 代码会继续运行,但festival.exe 不会继续运行。

What should I do to make festival.exe continue running?我应该怎么做才能让festival.exe 继续运行?

PS: festival.exe is a solver for sokoban. PS:festival.exe 是推箱子的求解器。 This case occurs when the given level cannot be solved.当无法解决给定级别时会发生这种情况。 Eg:例如:

########  
#.. $ $#
#  @   #
########

assuming you have the festival program like this:假设你有这样的节日节目:


from random import getrandbits


solved = getrandbits(1)
if solved:
    print("executed correctly")
    
else:
    print('a error ocurred')
    input('press a button to continue') 
    exit(1)

you can solve with:你可以解决:

from subprocess import Popen, PIPE


p = Popen(['python3','festival.py'],stdin=PIPE)
p.communicate(input=b'any\n')
  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM