stop python subprocess after n bytes or lines in proc.stdout.read() -


i'd run subprocess in python until subprocess has outputted number of bytes or lines. after point, i'd terminate it. possible subprocess?

here's have far:

proc = subprocess.popen(cmd, stdout=subprocess.pipe, stdin=subprocess.pipe, stderr=subprocess.pipe, shell=true) #kill after reaching n bytes of output, proc.terminate() out, errors = proc.communicate() 

thanks!

one straightforward way redirect output program byte/line counting you, example:

proc = subprocess.popen(cmd, stdout=subprocess.pipe, stdin=subprocess.pipe, stderr=subprocess.stdout, shell=true) head = subprocess.popen(['head', '-n', '20'], stdin=proc.stdout, stdout=subprocess.pipe, stderr=subprocess.pipe) proc.stdout.close() out, errors = head.communicate() 

i'm not sure how portable method is, able test on linux on windows should able use more command similar behavior.

closing proc.stdout necessary proc receives sigpipe when head exits.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -