我无法与由Popen创建的子过程交互 [英] I am unable to interact wih subprocess created by Popen

查看:130
本文介绍了我无法与由Popen创建的子过程交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows XP中使用python 2.5。
在这里,我使用子进程来运行我的shell,
现在我应该如何在shell中使用子进程运行
gdb。



我的代码:

  PID = subprocess.Popen('C:/STM/STxP70_Toolset_2010.2/bin/STxP70.bat' ,shell = True,stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE)。 

现在shell会打开,接下来的
如果我尝试使用

PID.communicate(gdb),



gdb未在shell中运行。



我应该为此做些什么。

解决方案

您的代码:


  1. 启动STxP70.bat
  2. 写入字符串gdb(不带结束换行符)输入并关闭标准输入。
  3. 正在读取它的输出直到文件结束。 PID.communicate不会让您与子进程进行任何进一步的交互,它会提供所提供的字符串,并收集所有输出,直到进程终止。

  4. 当STxP70.bat完成,子进程终止。

注意,如果shell将打开意味着一个新窗口出现一个shell提示,你拧了。这意味着STxP70.bat用'start'命令盯着它,你不能与它通信,因为它不会继承你的stdin / stdout / stderr管道。您必须创建您自己的批次修改,不会使用开始。




I am using python 2.5 in windows xp. In this i am using subprocess to run my shell, now how should i has to run gdb in shell using subprocess.

my code:

PID = subprocess.Popen('C:/STM/STxP70_Toolset_2010.2/bin/STxP70.bat', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).

Now shell will open, next if i try to run gdb using communicate by

PID.communicate ("gdb"),

"gdb" is not running in the shell.

What should i has to do for this.

解决方案

Your code:

  1. Starts STxP70.bat
  2. Writes string "gdb" (with no terminating newline) to it's standard input and closes the standard input.
  3. Is reading it's output until end of file. PID.communicate won't let you to interact with the subprocess any further—it writes the provided string and than collects all output until the process terminates.
  4. When STxP70.bat completes, the subprocess terminates.

Note, that if "shell will open" means a new window comes up with a shell prompt in it, you are screwed. It would mean the STxP70.bat stared it with 'start' command and you can't communicate with that, because it's not inheriting your stdin/stdout/stderr pipes. You would have to create your own modification of the batch that will not use 'start'.

.

这篇关于我无法与由Popen创建的子过程交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆