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

Is there any way determine the size of stdout stream?

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'.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.