Hi.
How can I get subprocess.Popen.stdout size?
Before reading from Popen.stdout I want to check its size, then get bytes from it.

def execute_bash(self):
            wait_sec = 5
            import time
            import subprocess
            wForceKill = True
            res = subprocess.Popen(self.command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            for i in range(wait_sec):
                res.poll()
                if res.returncode != None:
                    wForceKill = False
                    break
                time.sleep(1.0)

            if wForceKill: # inform about abort! 
                res.terminate()
                strm = res.stdout.detach()
                # here I need determine stdout's size!!!
                if stdout_size > 0:
                    res_out = strm.read(n)
                if res_out == None:
                    res_out = ""
                self.result = b"Subprocess was killed! Additional info:" + res_out
            else: # read output in normal terminate
                res_out = res.communicate()[0]
                self.result = res_out

Edited 6 Years Ago by _neo_: n/a

I think few people know this detach() method which is new in python 3. The answer is in the specification of the class of the returned stream. A pythonic way to do is to try to read from the stream: if it fails, it means that you can't read :)

Also note that if you want to write python 2 compatible code, you should avoid 'detach'.

Edited 6 Years Ago by Gribouillis: n/a

Thank you for your response.
Yep, you are right, now I'm reading data from stream, when there are data it's not problem, I can read them, but when stream is empty, it seems hang up. :( This is main problem :( Any other ideas?

Edited 6 Years Ago by _neo_: n/a

This question has already been answered. Start a new discussion instead.