Window watcher optimizations

I’d like to offer a suggestion to hopefully improve aw-watcher-window performance on Windows. This is part of a code I wrote time ago while playing with time tracking:

from win32gui import GetForegroundWindow
from win32process import GetWindowThreadProcessId
from psutil import Process

last_window = ""
last_pname = ""

def get_pname():
    global last_window
    global last_pname
    window = GetForegroundWindow()
    if window != last_window:
        pid = GetWindowThreadProcessId(window)[1]
        try:
            pname = Process(pid).name()
        except Exception:
            pname = "None"
        last_window, last_pname = window, pname
        return pname
    else:
        return last_pname

I don’t know if psutil is more efficient than wmi that you’re using, but I think storing the process name rather than figuring out what it is at every cycle might help.

This is windows only, so it would make it platform dependend. Is the performance of wmi really so bad/such a bottleneck?