相关疑难解决方法(0)

子进程 readline 挂起等待 EOF

我有一个简单的 C++ 程序,我试图通过 python 脚本执行它。(我对编写脚本非常陌生)并且我在通过管道读取输出时遇到问题。从我所见,如果没有 EOF,readline() 似乎无法工作,但我希望能够在程序中间读取并使脚本响应输出的内容。它不读取输出,而是挂起 python 脚本:

#!/usr/bin/env python
import subprocess
def call_random_number():
    print "Running the random guesser"
    rng = subprocess.Popen("./randomNumber", stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
    i = 50
    rng.stdin.write("%d\n" % i)
    output = rng.stdout.readline()
    output = rng.stdout.readline()

call_random_number()
Run Code Online (Sandbox Code Playgroud)

c++ 文件生成 1 到 100 之间的随机数,然后检查用户的猜测,直到他们猜对为止

#include<iostream>
#include<cstdlib>

int main(){
  std::cout<< "This program generates a random number from 1 to 100 and asks the user to enter guesses until they succuessfully guess the number.  It then tells the user how many …
Run Code Online (Sandbox Code Playgroud)

python subprocess stdout pipe readline

6
推荐指数
1
解决办法
9622
查看次数

标签 统计

pipe ×1

python ×1

readline ×1

stdout ×1

subprocess ×1