0

I am trying to use output of external program run using the run function. this program regularly throws a row of data which i need to use in mine script I have found a subprocess library and used its run()/check_output()

Example:
def usual_process(): # some code here for i in subprocess.check_output(['foo','$$']): some_function(i)

Now assuming that foo is already in a PATH variable and it outputs a string in semi-random periods.
I want the program to do its own things, and run some_function(i)every time foo sends new row to its output.

which boiles to two problems. piping the output into a for loop and running this as a background subprocess Thank you


Update: I have managed to get the foo output onto some_function using This

with os.popen('foo') as foos_output:
    for line in foos_output:
        some_function(line)

According to this os.popen is to be deprecated, but I am yet to figure out how to pipe internal processes in python Now just need to figure out how to run this function in a background

Community
  • 1
  • 1
Tomas
  • 111
  • 7
  • Do you mean `linux`? If so, which distro? – Anthony Kong Apr 09 '17 at 02:26
  • While I am programing and testing om mine Gentoo,
    I am trying to limit distro-specific code to minimum and stick to Pyton3 if possible. The resulting code will be mostly targeting Raspbian at first but in a latter version android. Intention is prity much to be updating the variable based on _foo_'s output, while the program does other things
    – Tomas Apr 09 '17 at 02:51

1 Answers1

0

SO, I have solved it. First step was to start the external script:
proc=Popen('./cisla.sh', stdout=PIPE, bufsize=1)

Next I have started a function that would read it and passed it a pipe

def foo(proc, **args):
       for i in proc.stdout:
         '''Do all I want to do with each'''

foo(proc).start()`

Limitations are: If your wish t catch scripts error you would have to pipe it in.
second is that it leaves a zombie if you kill parrent SO dont forget to kill child in signal-handling

Tomas
  • 111
  • 7