【发布时间】:2015-04-22 15:22:56
【问题描述】:
我使用的是 python 版本 2.7.9,当我尝试从 Popen 进程中读取一行时,它一直卡住,直到进程结束。如何在 stdin 结束之前读取它?
如果输入是“8200”(正确的密码),那么它会打印输出。 但是如果密码从'8200'改成没有输出,为什么?
子流程源码:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char password[10];
int num;
do
{
printf("Enter the password:");
scanf("%s", &password);
num = atoi(password);
if (num == 8200)
printf("Yes!\n");
else
printf("Nope!\n");
} while (num != 8200);
return 0;
}
Python 源码:
from subprocess import Popen, PIPE
proc = Popen("Project2", shell=True, stdin=PIPE,stdout=PIPE,stderr=PIPE)
#stdout_data = proc.communicate(input='8200\r\n')[0]
proc.stdin.write('123\r\n')
print proc.stdout.readline()
【问题讨论】:
标签: communicate