I received a complaint of sluggishness while a friend was testing out a gui I made. Sure enough, looking at Xp's performance monitor, it runs wide open consuming as much cpu as it can. After some digging around stackoverflow, it seems that this is normal expected behavior.
My question is this: Is there a way to limit or throttle the amount of resources a program is allowed to use? I kept the performance monitor visible while I opened a bunch of programs, and for the most part, all 'professional' programs (things like photoshop, sublime text, etc..) all seem to have an 'idle' state. That is, once started, after the initial peak their cpu usage dies down to a small fraction of the processor.
How does one go about limiting a python programs usage, or making it only grab the power when it needs to (like other programs appear to do)?
A slightly truncated version of my main loop:
while True:
events = pygame.event.get()
for event in events:
if event.type == QUIT:
if not flags['confirm']:
flags['alert'] = 1
else:
pygame.quit()
elif event.type == MOUSEBUTTONDOWN:
text_box.set_focus(event.button, event.pos)
m_numbar.set_focus(event.button, event.pos)
# print event.pos
if not flags['window_open']:
screen.blit(combined_bg, (0,0))
t_button.update(events, screen)
else:
screen.blit(combined_blur, (0,0))
if flags['config']:
screen.blit(config_window_img, (0,0))
text_box.update(events)
text_box.draw(screen)
m_numbar.update(events)
m_numbar.draw(screen)
submit.update(events, screen)
cancel.update(events, screen)
check_box.update(events, screen)
else:
text_box.draw(screen)
m_numbar.draw(screen)
if flags['alert']:
flags['window_open'] = True
screen.blit(alert_dialog, (0,0))
alert_cancel.update(events, screen)
alert_confirm.update(events, screen)
if flags['saving'][0]:
if time.time() - flags['saving'][1] < .75:
screen.blit(sav_img, (170,170))
else:
flags['window_open'] = False
flags['saving'][0] = False
if flags['currently_doing_thing']:
if not flags['alert']:
screen.blit(r_tag, (40,10))
if check_for_prog():
if not flags['prog_open']:
makeDir()
flags['prog_open'] = True
os.startfile("lla_.exe")
else:
flags['prog_open'] = False
if check_for_grab_process():
try:
os.system("TASKKILL /F /IM lla_.exe")
except:
pass
config_button.update(events, screen)
pygame.display.update()
In addition to Gui programming, is it possible to limit cpu usage on 'normal' tasks? For instance, while 1 will run at 100%cpu. Is there a way to throttle simple cases like this?