Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program slows my mac down
#1
I'm running a program that counts each keystroke I make. I'm trying to find some way to measure my productivity. The problem is that after 15 minutes the computer starts to really slow down. I have a Macbook Pro 2015. It is quite obvious that it is the program that is causing it because once it is stopped everything returns to normal. I don't see why counting time should slow the computer down any more than a program that counts keystrokes. The original program is written in python. Here it is:

import keyboard, threading
from pynput import mouse
import time
from time import sleep
from AppKit import NSWorkspace
from openpyxl import load_workbook
import datetime
import sys

#don't worry about this module, I built it my self and it just has a few obvious functions
from general_functions import get_hour_minute_sec, put_into_excel, \
    get_last_row

wb = load_workbook(str1 + "excel/keystrokes.xlsx")
ws = wb.worksheets[0]
last_row = get_last_row(ws, 500_000, 1)

# this logs the data into an excel spread sheet
def log_into_excel(keystrokes, mouse_strokes):
    global last_row
    thour = get_hour_minute_sec(datetime.datetime.now())
    if mouse_strokes == 0:
        last_row += 1
        put_into_excel(ws, last_row, 1, thour)
        put_into_excel(ws, last_row, 2, int(keystrokes / 2))
        print(f"keystrokes {int(keystrokes/2)}")
    else:
        put_into_excel(ws, last_row, 3, mouse_strokes)

    wb.save(str1 + "excel/keystrokes.xlsx")


def count_keystrokes():
    keystrokes = 0
    elapsed = 0
    total_elapsed = 0
    all_time = lambda x, y, z: y + (z - x) if x != 0 else 0

    while True:
        # only key strokes that occur when pycharm is frontmost are counted
        active_app = NSWorkspace.sharedWorkspace().activeApplication()
        front_app = active_app["NSApplicationName"]
        if front_app == 'PyCharm':
            put_to_sleep = False
            if elapsed == 0:
                elapsed = time.time()

            b = keyboard.read_key()
            keystrokes += 1

        else:
            put_to_sleep = True
            if elapsed > 0:
                total_elapsed += (time.time() - elapsed)
                elapsed = 0

        if all_time(elapsed, total_elapsed, time.time()) > 600:
            log_into_excel(keystrokes, 0)
            total_elapsed = 0
            elapsed = 0
            keystrokes = 0
        if put_to_sleep:
            time.sleep(2)


def on_click(x, y, button, pressed):
    if not pressed:
        return False


def count_mouse():
    #a separate module to count mouse clicks is needed
    all_time = lambda x, y, z: y + (z - x) if x != 0 else 0
    clicks = 0
    elapsed = 0
    total_elapsed = 0
    while True:
        with mouse.Listener(on_click=on_click, ) as listener:
            listener.join()
            frontmost = NSWorkspace.sharedWorkspace().activeApplication()
            if frontmost["NSApplicationName"] == 'PyCharm':
                if elapsed == 0:
                    elapsed = time.time()

                clicks += 1

                if all_time(elapsed, total_elapsed, time.time()) > 600:
                    log_into_excel(0, clicks)
                    total_elapsed = 0
                    elapsed = 0
                    clicks = 0

            else:
                if elapsed > 0:
                    total_elapsed += (time.time() - elapsed)
                    elapsed = 0

                if all_time(elapsed, total_elapsed, time.time()) > 600:
                    log_into_excel(0, clicks)
                    total_elapsed = 0
                    elapsed = 0
                    clicks = 0

                time.sleep(2)


t1 = threading.Thread(target=count_mouse)
t2 = threading.Thread(target=count_keystrokes)
t1.daemon = True
t2.daemon = True
t1.start()
t2.start()

while True:
    sleep(0.05)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cython np.where slows down the code Johanoosterwaal 1 1,728 Sep-01-2021, 07:33 AM
Last Post: Johanoosterwaal
  Random access binary files with mmap - drastically slows with big files danart 1 3,954 Jun-17-2019, 10:45 AM
Last Post: danart

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020