piping binary data between python and c++

前端 未结 1 1589
青春惊慌失措
青春惊慌失措 2021-01-01 06:21

I have been working on a plugin to QGIS using Python 2.7 which works fine until I actually go to do some image processing on the mapped layers. Even simple tasks like collec

相关标签:
1条回答
  • 2021-01-01 06:57

    The problem probably is buffering: by default, the C stdio buffers everything written to stdout and flushes the buffer back into the pipe only when a newline is written (line buffering). The problem disappears when you call fflush(stdout) after writing. You can also disable (or control) buffering via the setvbuf function defined in <stdio.h>, e.g. use setvbuf(stdout, NULL, _IONBF, 0) to disable buffering completely.

    I have tested the first variant with the following two programs:

    import subprocess
    
    proc = subprocess.Popen("./echotest",
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE)
    
    proc.stdin.write('abc')
    message = proc.stdout.read(3)
    print "return message ->" + message + " written by python \n" 
    proc.stdin.write('q')
    proc.wait()
    

    and a little C program:

    #include <stdio.h>
    
    int main (int argc, char **argv) {
        for (;;) {
            char buf;
            fread(&buf, 1, 1, stdin);
            if ('q' == buf)
                break;
            fwrite(&buf, 1, 1, stdout);
            fflush(stdout);
        }
    
        return 0;
    }
    

    Note that you have to specify how many bytes you want to read back from the subprocess or the program will block, waiting for more output. If this bothers you, try one of the solutions to this question.

    0 讨论(0)
提交回复
热议问题