Search code examples
pythonservice

In Python, imported function has the same value, but it should not


import psutil

def check_cpu_usage():
    usage = psutil.cpu_percent(1)
    if usage > 75:
        print("CPU usage is AWFUL.")
    else:
        print("ok1")

def check_frequency():
    frequency = psutil.cpu_freq()
    if frequency.current > 900:
        print("Frequency is HIGH.")
    else:
        print("ok2")

def check_vmemory():
    vmemory = psutil.virtual_memory()
    if vmemory.percent > 75:
        print("Additional memory is NEEDED.")
    else:
        print("ok3")

def check_disk_usage():
    usaged = psutil.disk_usage('/')
    if usaged.percent > 70:
        print("Disk is going to be FULL.")
    else:
        print("ok4")

def check_battery():
    battery = psutil.sensors_battery()
    if battery.percent < 30:
        print("plug in")
    else:
        print("ok5")

It was the urll.py file whose check_vmemory module has been exported.

import os 
import win32serviceutil
import wmi
from urll import check_vmemory

if check_vmemory != "ok3":
    service_name = ['Print Spooler', 'Windows Update', 'HP SI Service']
    for s in service_name:
        win32serviceutil.StopService(s)

But the result is the same. Even though I am changing the parameters to see the result, it always act like the check_vmemory() != "ok3" is True What is the reason for that?


Solution

  • You have to have check_vmemory return a string:

    def check_vmemory():
      vmemory = psutil.virtual_memory()
      result = ""
      if vmemory.percent > 75:
        print("Additional memory is NEEDED.")
      else:
        print("ok3")
        result = "ok3"
      return result
    

    Then call the check_vmemory function in your condition (with parentheses ())

    if check_vmemory() != "ok3":
      ...
    

    check_vmemory() will return either '' or 'ok3'. check_vmemory is the function itself, so the condition check_vmemory != 'ok3' is always True.