Python Gui and CPU Usage. How do I create an "idle" or "choke" so a piece of code doesn't consume 100 available CPUs?
I got a complaint about slowness when a friend was testing a gui that I made. Surely looking at the Xp performance monitor it runs at wide open draws, as is often the case. After some digging around stackoverflow, this seems to be the normal expected behavior.
My question is this: is there a way to limit or reduce the amount of resources a program is allowed to use? I kept the performance monitor while I was opening a bunch of programs, and for the most part all "professional" programs (like photoshop, sublime text, etc.) seem to have an "unoccupied" state. That is, once started, after the initial peak, their CPU usage is reduced to a small fraction of the CPU.
How can you limit yourself to using python programs, or force it to only seize power when necessary (like other programs, for example)?
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 example, it while 1
will run at 100% speed. Is there a way to throttle such simple cases?
source to share
When using Pygame, your main loop will look something like this (from the Python Pygame Introduction ):
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()
Notice the call pygame.event.get()
at the top of the loop. Under normal circumstances, when nothing happens, this function will block when called. This means that your script will wait for the function to return event.get()
without using any processor at all. The function event.get()
only returns when you need something interesting (moving the mouse, pressing a key, etc.).
The description of your problem makes me think you are not using pygame.event.get()
as it is intended to be used. Perhaps you can show what your main loop looks like.
source to share