python - How to concatenate the output of several processes into the input of another? -


i'm writing script executes list of processes , concatenates of output input of process. i've condensed script test case using echo , cat stand-ins actual processes.

#!/usr/bin/python  import os,subprocess  (pipeout, pipein) = os.pipe()  catprocess = subprocess.popen("/bin/cat", stdin = pipeout)  line in ["first line", "last line"]:     subprocess.call(["/bin/echo",line], stdout = pipein)  os.close(pipein) os.close(pipeout)  catprocess.wait() 

the program works expected, except call catprocess.wait() hangs (presumably because it's still waiting more input). passing close_fds=true popen or call doesn't seem help, either.

is there way close catprocesses's stdin exits gracefully? or there way write program?

passing close_fds=true catprocess helps on system.

you don't need create pipe explicitly:

#!/usr/bin/python subprocess import popen, pipe, call  cat = popen("cat", stdin=pipe) line in ["first line", "last line"]:     call(["echo", line], stdout=cat.stdin) cat.communicate() # close stdin, wait 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -