English 中文(简体)
shed <p = Popen([“command”], endout=PIPE> 当p.stdout时(-1)被阻断。
原标题:Python <p = Popen(["command"], stdout=PIPE)> when p.stdout.read(-1) is blocked

hello i在“灰色”处理模块中有问题

我想与儿童进程互动(ex:儿童发育、辍学)。

这是儿童方案法典。

  1 #include <stdio.h>
  2 
  3 int main(){
  4         char buf[1024];
  5         printf("asdfsadfasff
");
  6         scanf("%s",buf);
  7         printf("%s
",buf);
  8 }

该守则非常简单,印刷了插图、等待投入和印刷投入,以显示:

下面是《 p法》。

  1 #!/usr/bin/python
  2 from subprocess import *
  3 
  4 fd = Popen(["./random"],stdin=PIPE,stdout=PIPE)
  5 print "pid = %d"%(fd.pid) # print pid
  6 result = ""
  7 result = fd.stdout.read(-1) # read
  8 print result
  9 fd.stdin.write("write write write!!!
")
 10 result = fd.stdout.read(-1)
 11 print result

在这种情况下,我预计这一方案将很好地发挥作用,但第7行(fd.stdout.read(-1)被封锁,从来不工作。

我将职能部分改为职能部分(改为(-1),改为(1),改为(1024),但所有工作都不奏效。

但是,在先给予扼杀时,方案是行之有效的。

我认为,当方案结束时,儿童 st忙的缓冲未能补足...... 这只是我的意见。

这一问题是否有任何解决办法?

Edit1. when I execute program which is print string first, and wait user s input such as "sudo su" This worked well that I expected

我无法理解为什么这一案件运作良好。

  1 from subprocess import *
  2 
  3 fd = Popen(["sudo","su"],stdin=PIPE,stdout=PIPE)
  4 
  5 print fd.stdout.read(-1)
  6 print fd.stdin.write("asdfasdfas
")
最佳回答

The child process ./random is using stdio, which by default does "full buffering" rather than "line buffering" when the process s stdout is not a tty device. Since python runs the process via pipes (which are not tty devices) the program is buffering its output, and not printing it at the time of the scanf call. The buffer is flushed (written to stdout) only (a) when it fills up; (b) when explicitly flushed with an fflush call; or (c) at program exit.

您可修改儿童程序,以明确fflush。 电话或您可使用一种pseudo-tty,例如,pexpectsh <<<>>>>>>><<>>>>>>><>><>>>>><>>><>>>>>>>>>>>>><>>>>>>><>>>>>><>>>>>>>>>>>>><>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> (说明<代码>sh 模块因缺省而只使用一定数量的产出,但通常足够。 或者,在这样的情况下,如果你只管理“互动”方案(但实际上完全可以预测),那么你就能够按事先规定的已知投入顺序进行:

fd = Popen(["./random"], stdin=PIPE, stdout=PIPE)
result = fd.communicate("write write write!!!
")[0]

虽然你当然会把所有排他性投入混为一谈,并且必须将其印刷的两条线分开(就本案而言)。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签