projectatomic / adb-utils

A set of utilities for managing services used provided in the Atomic Developer Bundle.
GNU General Public License v2.0
13 stars 22 forks source link

Using pass results in to a CPU intensive operation #197

Closed LalatenduMohanty closed 7 years ago

LalatenduMohanty commented 7 years ago
def service_status(service_name):
    if service_name == "kubernetes":
        status = 0
        services = ['etcd', 'kube-apiserver', 'kube-controller-manager',
                    'kube-scheduler', 'kube-proxy', 'kubelet']
        for service in services:
            while system("systemctl is-active %s" % service)[0].strip() == 'activating':
                pass
            status = status or system("systemctl is-active %s" % service)[2]
            if status:
                service_stop(service_name)
                break
        return status
    else:
        while system("systemctl is-active %s" % service_name)[0].strip() == 'activating':
            pass
        return system("systemctl is-active %s" % service_name)[2]

Using pass results in to a very CPU intensive operation. it is null operation, so it will continuously run systemctl is-active %s" % service) till it comes out of the loop. If you do

while True:
   pass

It takes 100% CPU. time.sleep(1) is a better option and less CPU intensive.

[1] https://github.com/projectatomic/adb-utils/blob/master/utils/sccli.py#L117-L133