<bdo id='LuWWW'></bdo><ul id='LuWWW'></ul>
  • <legend id='LuWWW'><style id='LuWWW'><dir id='LuWWW'><q id='LuWWW'></q></dir></style></legend>
    <tfoot id='LuWWW'></tfoot>

    <i id='LuWWW'><tr id='LuWWW'><dt id='LuWWW'><q id='LuWWW'><span id='LuWWW'><b id='LuWWW'><form id='LuWWW'><ins id='LuWWW'></ins><ul id='LuWWW'></ul><sub id='LuWWW'></sub></form><legend id='LuWWW'></legend><bdo id='LuWWW'><pre id='LuWWW'><center id='LuWWW'></center></pre></bdo></b><th id='LuWWW'></th></span></q></dt></tr></i><div id='LuWWW'><tfoot id='LuWWW'></tfoot><dl id='LuWWW'><fieldset id='LuWWW'></fieldset></dl></div>
    1. <small id='LuWWW'></small><noframes id='LuWWW'>

        子进程 readline 挂起等待 EOF

        时间:2023-07-22

                <bdo id='5jOYQ'></bdo><ul id='5jOYQ'></ul>

                <small id='5jOYQ'></small><noframes id='5jOYQ'>

                  <tbody id='5jOYQ'></tbody>
                <i id='5jOYQ'><tr id='5jOYQ'><dt id='5jOYQ'><q id='5jOYQ'><span id='5jOYQ'><b id='5jOYQ'><form id='5jOYQ'><ins id='5jOYQ'></ins><ul id='5jOYQ'></ul><sub id='5jOYQ'></sub></form><legend id='5jOYQ'></legend><bdo id='5jOYQ'><pre id='5jOYQ'><center id='5jOYQ'></center></pre></bdo></b><th id='5jOYQ'></th></span></q></dt></tr></i><div id='5jOYQ'><tfoot id='5jOYQ'></tfoot><dl id='5jOYQ'><fieldset id='5jOYQ'></fieldset></dl></div>

              • <legend id='5jOYQ'><style id='5jOYQ'><dir id='5jOYQ'><q id='5jOYQ'></q></dir></style></legend>

                <tfoot id='5jOYQ'></tfoot>
                  本文介绍了子进程 readline 挂起等待 EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个简单的 c++ 程序,我正在尝试通过 python 脚本执行该程序.(我对编写脚本非常陌生)并且我无法通过管道读取输出.从我所看到的情况来看,如果没有 EOF,readline() 似乎将无法工作,但我希望能够在程序中间读取并让脚本响应输出的内容.而不是读取输出,它只是挂起python脚本:

                  I have a simple c++ program that I'm trying to execute through a python script. (I'm very new to writing scripts) and I'm having trouble reading output through the pipe. From what I've seen, it seems like readline() won't work without EOF, but I want to be able to read in the middle of the program and have the script respond to whats being outputted. Instead of reading output, it just hangs the python script:

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

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

                  and the c++ file, which generates a random number between one and 100, then checks the users guess until they guess correctly

                  #include<iostream>
                  #include<cstdlib>
                  using namespace std;
                  
                  int main(){
                    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 guesses it took them"<<endl;
                    srand(time(NULL));
                    int num=rand()%100;
                    int guessCount=0;
                    int guess=-1;
                    cout<<"Please enter a number:  ";
                    cin>>guess;
                    while(guess!=num){
                      if(guess>num){cout<<"That guess is too high.  Please guess again:  ";}
                      else{cout<<"That guess is too low.  Please guess again:  ";}
                      cin>>guess;
                      guessCount++;
                    }
                    cout<<"Congratulations!  You solved it in "<<guessCount<<" guesses!"<<endl;
                    return 0;
                  }
                  

                  最终目标是让脚本通过二进制搜索解决问题,但现在我只想能够读取一行而不是文件末尾

                  the eventual goal is to have the script solve the problem with a binary search, but for now I just want to be able to read a line without it being the end of the file

                  推荐答案

                  正如 @Ron Reiter 指出的,你可以'不要使用 readline() 因为 cout 不会隐式打印换行符 -- 你需要 std::endl" "在这里.

                  As @Ron Reiter pointed out, you can't use readline() because cout doesn't print newlines implicitly -- you either need std::endl or " " here.

                  对于交互式使用,当您无法更改子程序时,pexpect 模块 提供了几种方便的方法(通常 它解决了免费:直接从/到终端的输入/输出(在标准输入/标准输出之外)和块缓冲问题):

                  For an interactive use, when you can't change the child program, pexpect module provides several convenience methods (and in general it solves for free: input/output directly from/to terminal (outside of stdin/stdout) and block-buffering issues):

                  #!/usr/bin/env python
                  import sys
                  
                  if sys.version_info[:1] < (3,):
                      from pexpect import spawn, EOF # $ pip install pexpect
                  else:
                      from pexpect import spawnu as spawn, EOF # Python 3
                  
                  child = spawn("./randomNumber") # run command
                  child.delaybeforesend = 0 
                  child.logfile_read = sys.stdout # print child output to stdout for debugging
                  child.expect("enter a number: ") # read the first prompt
                  lo, hi = 0, 100
                  while lo <= hi:
                      mid = (lo + hi) // 2
                      child.sendline(str(mid)) # send number
                      index = child.expect([": ", EOF]) # read prompt
                      if index == 0: # got prompt
                          prompt = child.before
                          if "too high" in prompt:
                              hi = mid - 1 # guess > num
                          elif "too low" in prompt:
                              lo = mid + 1 # guess < num
                      elif index == 1: # EOF
                          assert "Congratulations" in child.before
                          child.close()
                          break
                  else:
                      print('not found')
                      child.terminate()
                  sys.exit(-child.signalstatus if child.signalstatus else child.exitstatus)
                  

                  它可以工作,但它是二进制搜索,因此 (传统上)可能存在错误.

                  It works but it is a binary search therefore (traditionally) there could be bugs.

                  这是一个类似的代码,使用 subprocess 模块进行比较:

                  Here's a similar code that uses subprocess module for comparison:

                  #!/usr/bin/env python
                  from __future__ import print_function
                  import sys
                  from subprocess import Popen, PIPE
                  
                  p = Popen("./randomNumber", stdin=PIPE, stdout=PIPE,
                            bufsize=1, # line-buffering
                            universal_newlines=True) # enable text mode
                  p.stdout.readline() # discard welcome message: "This program gener...
                  
                  readchar = lambda: p.stdout.read(1)
                  def read_until(char):
                      buf = []
                      for c in iter(readchar, char):
                          if not c: # EOF
                              break
                          buf.append(c)
                      else: # no EOF
                          buf.append(char)
                      return ''.join(buf).strip()
                  
                  prompt = read_until(':') # read 1st prompt
                  lo, hi = 0, 100
                  while lo <= hi:
                      mid = (lo + hi) // 2
                      print(prompt, mid)
                      print(mid, file=p.stdin) # send number
                      prompt = read_until(':') # read prompt
                      if "Congratulations" in prompt:
                          print(prompt)
                          print(mid)
                          break # found
                      elif "too high" in prompt:
                          hi = mid - 1 # guess > num
                      elif "too low" in prompt:
                          lo = mid + 1 # guess < num
                  else:
                      print('not found')
                      p.kill()
                  for pipe in [p.stdin, p.stdout]:
                      try:
                          pipe.close()
                      except OSError:
                          pass
                  sys.exit(p.wait())
                  

                  这篇关于子进程 readline 挂起等待 EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在运行时拦截子进程的标准输出 下一篇:以与长时间运行的 Python 进程不同的用户身份运行子进程

                  相关文章

                  1. <small id='iKZfh'></small><noframes id='iKZfh'>

                      <bdo id='iKZfh'></bdo><ul id='iKZfh'></ul>

                    <i id='iKZfh'><tr id='iKZfh'><dt id='iKZfh'><q id='iKZfh'><span id='iKZfh'><b id='iKZfh'><form id='iKZfh'><ins id='iKZfh'></ins><ul id='iKZfh'></ul><sub id='iKZfh'></sub></form><legend id='iKZfh'></legend><bdo id='iKZfh'><pre id='iKZfh'><center id='iKZfh'></center></pre></bdo></b><th id='iKZfh'></th></span></q></dt></tr></i><div id='iKZfh'><tfoot id='iKZfh'></tfoot><dl id='iKZfh'><fieldset id='iKZfh'></fieldset></dl></div>
                    <tfoot id='iKZfh'></tfoot>

                    1. <legend id='iKZfh'><style id='iKZfh'><dir id='iKZfh'><q id='iKZfh'></q></dir></style></legend>