python read from shell command output

Kamarkaka

I understand you might think this is a duplicated question, but so far I did not find solutions to my own problem.

Currently, I have a C program continuously producing streams of data. I hope there is a way for my python program can read those data so I don't have to finish everything in C.

The C program mainly feels like this:

int i = 0;
while(1){
    printf("%d %d\n", i, i+1);
    i++;
}

I read and tried the subprocess in Python, seems they all need to wait for command to complete.(for example this: Constantly print Subprocess output while process is running) I am hoping some buffer mechanism so I can record this bit stream and process it line by line. Thanks.

Several assumptions:

1) processed line can be discarded

2) buffer/queue size can be no problem since I can control the rate from source to match the processing speed of python program.

A little more background: The C program basicly drives a camera feed (and that's why it is written in C), does some OpenCV stuff and outputs an object position (x, y) int value. The python program needs the position to do some further processing.

Thanks.

John1024

Here's a way to process line by line the output of a c (or shell) program with the subprocess module:

from subprocess import Popen, PIPE
# Set up the process
p = Popen("yourprogram", stdout=PIPE, close_fds=True)
count = 0
while True:
    count += 1
    print '%s: %s' % (count, p.stdout.readline().strip())

For demonstration purposes, yourprogram can be a simple shell script (with the executable bit set):

#!/bin/sh
while sleep 1s
do
    date
done

Running the python program produces:

$ python test.py
1: Thu Dec  5 23:31:45 PST 2013
2: Thu Dec  5 23:31:46 PST 2013
3: Thu Dec  5 23:31:47 PST 2013
4: Thu Dec  5 23:31:48 PST 2013
5: Thu Dec  5 23:31:49 PST 2013
6: Thu Dec  5 23:31:50 PST 2013
7: Thu Dec  5 23:31:51 PST 2013

It continues until you terminate it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Shell Scripting: read input from command output

From Dev

Read output of shell command in meteor

From Dev

Log all output from a shell command

From Dev

Shell output from one command as input to other

From Dev

Log all output from a shell command

From Dev

How to capture output from bourne shell command

From Dev

Running shell command in python and reading output

From Dev

python Save the output of a shell command into a text file

From Dev

Running shell command in python and reading output

From Dev

Is there any way to read lines from command output?

From Dev

Execute Shell Command, and read it's output in Chrome Packaged Apps

From Dev

how to read multiline output command line by line, in a shell script?

From Dev

Execute Shell Command, and read it's output in Chrome Packaged Apps

From Dev

Python read windows Command Line output

From Dev

output Command Shell in if statment

From Dev

Execute command on shell command output

From Dev

Call python from shell and capture output

From Dev

Shell command fails from python, ok from shell

From Dev

Shell command fails from python, ok from shell

From Dev

WScript.Shell.Exec - read output from stdout

From Dev

In shell, print only the specific line/word from command's output

From Dev

How to capture newlines from command substitution output in fish shell?

From Dev

How to enable colored output from shell command wrapped in a script?

From Dev

Add info to output -- obtained from a shell command execution

From Dev

Not able to get any output from awk command in shell script

From Dev

How to retrieve output from Eggplant shell script command

From Dev

Microsoft nmake: Is it possible to define macros from shell command output?

From Dev

How to hidden the output of Linux command launched from shell_exec()

From Dev

Shell programming: Select random line from command output

Related Related

  1. 1

    Shell Scripting: read input from command output

  2. 2

    Read output of shell command in meteor

  3. 3

    Log all output from a shell command

  4. 4

    Shell output from one command as input to other

  5. 5

    Log all output from a shell command

  6. 6

    How to capture output from bourne shell command

  7. 7

    Running shell command in python and reading output

  8. 8

    python Save the output of a shell command into a text file

  9. 9

    Running shell command in python and reading output

  10. 10

    Is there any way to read lines from command output?

  11. 11

    Execute Shell Command, and read it's output in Chrome Packaged Apps

  12. 12

    how to read multiline output command line by line, in a shell script?

  13. 13

    Execute Shell Command, and read it's output in Chrome Packaged Apps

  14. 14

    Python read windows Command Line output

  15. 15

    output Command Shell in if statment

  16. 16

    Execute command on shell command output

  17. 17

    Call python from shell and capture output

  18. 18

    Shell command fails from python, ok from shell

  19. 19

    Shell command fails from python, ok from shell

  20. 20

    WScript.Shell.Exec - read output from stdout

  21. 21

    In shell, print only the specific line/word from command's output

  22. 22

    How to capture newlines from command substitution output in fish shell?

  23. 23

    How to enable colored output from shell command wrapped in a script?

  24. 24

    Add info to output -- obtained from a shell command execution

  25. 25

    Not able to get any output from awk command in shell script

  26. 26

    How to retrieve output from Eggplant shell script command

  27. 27

    Microsoft nmake: Is it possible to define macros from shell command output?

  28. 28

    How to hidden the output of Linux command launched from shell_exec()

  29. 29

    Shell programming: Select random line from command output

HotTag

Archive