Binding / piping output of run() on/into function in python3 (lynux)

拜拜、爱过 提交于 2019-12-12 05:58:02

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/43302064/binding-piping-output-of-run-on-into-function-in-python3-lynux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!