Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limited buffer in Popen [duplicate]

I am launching a script using a python code. This code should launch the script which writes a file on the disk, and wait for the script to finish.

But whenever I launch this script using python, the result file doesn't exceed 65768 bytes and the script doesn't respond anymore. Here is what I use in python :

p = subprocess.Popen(command, 
                     shell=True, 
                     stdout=subprocess.PIPE, 
                     stderr=subprocess.STDOUT, 
                     bufsize=-1)
p.wait()

where command is the command for the script.

Does anyone know a solution for this issue ?

Thank you.

like image 396
user3116130 Avatar asked Feb 05 '26 09:02

user3116130


1 Answers

from time import sleep
p = subprocess.Popen(command, 
                     shell=True, 
                     stdout=subprocess.PIPE, 
                     stderr=subprocess.STDOUT, 
                     bufsize=-1)
output = ''
while p.poll() is None:
    output += p.stdout.readline()+'\n'  # <--- This is your magic
    sleep(0.025)
output += p.stdout.read() # <--- And this is just to get the leftover data
print('Command finished')

p.stdout.close()

as @J.F comments on almost every single Popen answer i give, you should never define stdout=..., stdin=... or stderr=... unless you're going to utelize them. Because they will fill up the buffer and hang your application.

But if you do, make sure you "tap" from it once in a while.

like image 67
Torxed Avatar answered Feb 06 '26 23:02

Torxed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!