gpt4 book ai didi

python - 简单的 pythoncurses-application 在运行时使用 100% CPU。这是正常的吗?

转载 作者:行者123 更新时间:2023-11-30 23:37:20 26 4
gpt4 key购买 nike

我正在构建这个简单的应用程序,我注意到当curses运行时,“top”中的CPU负载是100%..这看起来很糟糕。谁能解释一下吗?

def Run(self, stdscr):

self.screen = stdscr

curs_set(0)
self.screen.nodelay(1)
self.y_max = self.screen.getmaxyx()[0]
self.x_max = self.screen.getmaxyx()[1]

for e in self.Inv:
self.Create_Win(self.Inv.index(e), e)

self.test_win = self.screen.derwin(1, self.x_max, 10, 0)
self.test_win.addstr(self.TargetDict["Target_1"]["Obj"].Name)

running = True
while running:
self.screen.refresh()
q = self.screen.getch()


#Press q to exit program
if q == ord("q"):
running = False

enter image description here

enter image description here

这是程序运行时的样子。一旦我用“q”终止程序,它就会恢复正常。

最佳答案

问题是对getch的调用不是阻塞的,因此程序不断地循环通过while循环。来自 getch documentation :

Get a character. Note that the integer returned does not have to be in ASCII range: function keys, keypad keys and so on return numbers higher than 256. In no-delay mode, -1 is returned if there is no input, else getch() waits until a key is pressed.

您的程序在循环之前设置nodelay:

self.screen.nodelay(1)

关于python - 简单的 pythoncurses-application 在运行时使用 100% CPU。这是正常的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15686811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com