curiousguy
curiousguy

Reputation: 3262

Python Subprocess efficiently check if child process terminated

I am running this bash cmd using subprocess to start jmeter in a remote system.

['java', '-server', '-XX:+HeapDumpOnOutOfMemoryError', '-Xms512m', '-Xmx512m', '-XX:+UseG1GC', 
'-XX:MaxGCPauseMillis=250', '-XX:G1ReservePercent=20', '-Djava.security.egd=file:/dev/urandom', 
'-Xms1024m', '-Xmx1024m', '-jar', '/jmeter/apache-jmeter-4.0/bin/ApacheJMeter.jar', '-n', 
'-t', '/code/jmx_file.jmx', '-JReplica_ID=1', '-JService_Name=execution-b063818a',
'-l', '/code/logs/logs.log']

Now I am using while loop to check the child processes are terminated are not and if terminated flush log files like below

sub_proc = subprocess.Popen(
                    bash_cmd,
                    stdout=proc_log,
                    stderr=proc_log
                )
# while the process is still running, dump the data to the file.
while sub_proc.poll() is None:
    proc_log.flush()
    os.fsync(proc_log)
sub_proc.wait()  

But this while loop is shooting my process to 100% CPU utilization .

I want to know if there is any other way to efficiently check if child process terminated or not like above to reduce the CPU usage.

Upvotes: 0

Views: 816

Answers (1)

Dusan Gligoric
Dusan Gligoric

Reputation: 584

Add sleep to your while loop, otherwise its bound to use as much CPU as it can:

from time import sleep
sub_proc = subprocess.Popen(
                    bash_cmd,
                    stdout=proc_log,
                    stderr=proc_log
                )
# while the process is still running, dump the data to the file.
while sub_proc.poll() is None:
    proc_log.flush()
    os.fsync(proc_log)
    sleep(0.05) #change to acceptable value
sub_proc.wait()  

You are checking if its running with sub_proc.poll() is None in a correct manner.

Upvotes: 1

Related Questions