Sep-03-2018, 01:43 PM
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)