相关疑难解决方法(0)

subprocess.Popen.stdout - 实时读取标准输出(再次)

同样,同样的问题.
原因是 - 阅读以下内容后仍然无法使其正常工作:

我的情况是我有一个用C编写的控制台应用程序,让我们在循环中采用这个代码:

tmp = 0.0;   
printf("\ninput>>"); 
scanf_s("%f",&tmp); 
printf ("\ninput was: %f",tmp); 
Run Code Online (Sandbox Code Playgroud)

它不断读取一些输入并写入一些输出.

我与它交互的python代码如下:

p=subprocess.Popen([path],stdout=subprocess.PIPE,stdin=subprocess.PIPE)
p.stdin.write('12345\n')
for line in p.stdout: 
    print(">>> " + str(line.rstrip())) 
    p.stdout.flush() 
Run Code Online (Sandbox Code Playgroud)

到目前为止,每当我读取表单时,p.stdout它总是等待,直到进程终止然后输出一个空字符串.我尝试了很多东西 - 但结果仍然相同.

我尝试过Python 2.6和3.1,但版本没关系 - 我只需要让它在某个地方工作.

python subprocess stdout popen

12
推荐指数
1
解决办法
1万
查看次数

Python运行一个守护进程子进程并读取stdout

我需要运行一个程序并将其输出收集到stdout.这个程序(socat)需要在python脚本的持续时间内在后台运行.一旦它运行,Socat就会处于dameon模式,但首先它会向stdout输出一些我需要用于其余脚本的行.

命令: socat -d -d PTY: PTY:

输出:

2011/03/23 21:12:35 socat[7476] N PTY is /dev/pts/1
2011/03/23 21:12:35 socat[7476] N PTY is /dev/pts/2
2011/03/23 21:12:35 socat[7476] N starting data transfer loop with FDs [3,3] and [5,5]
Run Code Online (Sandbox Code Playgroud)

...

我基本上想在我的程序开始时运行它并让它一直运行直到脚本终止,但我需要将两个/ dev/pts/X名称读入python.

谁能告诉我怎么做?

我想出了这个只是挂起,我猜是因为它阻止子进程终止.

#!/usr/bin/python
from subprocess import Popen, PIPE, STDOUT

cmd = 'socat -d -d PTY: PTY: &'

p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
output = p.stdout.read()

# Process the output 
print(output)
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

编辑:似乎它可能写入stderr,但脚本仍然只是在有和没有从stderr读取甚至没有读取.

python

12
推荐指数
1
解决办法
1万
查看次数

持久的python子进程

有没有办法在python"持久"中进行子进程调用?我正在调用一个需要一段时间来加载多次的程序.因此,如果我可以让该程序保持打开并与之通信而不会将其删除,那将会很棒.

我的python脚本的卡通版本如下所示:

for text in textcollection:
    myprocess = subprocess.Popen(["myexecutable"],
                stdin = subprocess.PIPE, stdout = subprocess.PIPE,
                stderr = None)
    myoutputtext, err = myprocess.communicate(input=text)
Run Code Online (Sandbox Code Playgroud)

我需要单独处理每个文本,因此将它们全部加入一个大文本文件并处理一次不是一个选项.

最好是,如果有这样的选项

myprocess = subprocess.Popen(["myexecutable"],
            stdin = subprocess.PIPE, stdout = subprocess.PIPE,
            stderr = None)    for text in textcollection:
for text in textcollection:
    myoutputtext, err = myprocess.communicate(input=text)
Run Code Online (Sandbox Code Playgroud)

我可以把这个过程打开,我真的很感激.

python subprocess

10
推荐指数
2
解决办法
2万
查看次数

有没有办法使文件描述符在Windows中非阻塞?

我想将我的代码从linux移植到windows。它是这样的:

void SetNonBlocking( int filehandle )
{
    int fhFlags;

    fhFlags = fcntl(filehandle,F_GETFL);
    if (fhFlags < 0)
    {
        perror("fcntl(F_GETFL)");
        exit(1);
    }

    fhFlags |= O_NONBLOCK;
    if (fcntl(filehandle,F_SETFL,fhFlags) < 0)
    {   
        perror("fcntl(F_SETFL)");
        exit(1);
    } 

    return;
}
Run Code Online (Sandbox Code Playgroud)

现在我想在 Windows 中也有同样的东西。有任何想法吗?实际上,我的文件句柄是通过 WinApi 方法创建的管道的读取端CreatePipe

c winapi file

5
推荐指数
1
解决办法
5401
查看次数

标签 统计

python ×3

subprocess ×2

c ×1

file ×1

popen ×1

stdout ×1

winapi ×1