English 中文(简体)
Python Gui 编程和使用 cpu 。 如何制造闲置或节流, 使代码不能消耗100个可用的 cpu?
原标题:Python Gui programming and cpu usage. How does one create an idle or throttle so a piece of code does not consume 100 of the available cpu?

一个朋友在测试我制作的仪表时,我收到了一个疲软的抱怨。 当然,看Xp S的性能监视器,它会尽可能地开阔地消费。在对堆积流进行挖掘之后,这似乎是正常的预期行为。

我的问题是:是否有办法限制或削减一个程序允许使用的资源数量?我打开了一大堆程序时,我保持了性能监视器的可见度,大部分情况下,所有专业程序(如光电厂、高精度文本等)似乎都处于闲置状态。 也就是说,一旦启动,在最初的峰值后,它们的 cpu 使用量就下降到了处理器的一小部分。

如何限制 python 程序的使用, 或只让它在需要时抓住权力( 与其他程序似乎一样)?

我主环略微短略的版本 :

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()

除了 Gui 编程之外, 能否限制正常任务的 cpu 使用? 例如, 1 < code > 将运行到 100% cpu 。 是否有一种方法可以加速这样的简单案例?

问题回答

当使用 Pygame 时, 您的主环将看起来像这样( 从 < a href=" http://www.pygame.org/docs/ tut/intro/intro. html" rel = "nofollow" > Python Pygame importation ):

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()

注意在循环顶端 pygame. event.get () 调用到 game. event.get () 调用到循环顶端。 在正常情况下, 没有发生其它事情的情况下, 此函数会调用 < em> block 。 这意味着您的脚本会等待 < code> event. get () 函数返回, 而不会使用 CPU 。 event.get () 函数只有在您需要处理一些有趣的问题( 移动、 按键等) 时才返回 。

您问题的描述让我相信您没有使用 pygame. event. get () 的原意。 或许您可以显示您主环的外观 。

GUI 循环的闲置回调总是被调用, 除非被明确告知不调用它( 通常通过调用本身, 通常通过回调返回假 ) 。 只要保持它大部分时间禁用它, 并且只有在发生需要它运行的事件时才允许它运行 。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签