Pergunta

Im building this simple application and i noticed that while curses is running the CPU-load in "top" is 100%.. this seems bad. Can anyone explain this?

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

This is how the program looks while its running. As soon as i terminate the programwith 'q' it goes down to normal.

Foi útil?

Solução

The problem is that the call to getch is not blocking, so the program is constantly cycling through the while loop. From the 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.

Your program sets nodelay prior to the loop:

self.screen.nodelay(1)

Outras dicas

Where nodelay(True) is needed, use built in sleep function

curses.napms(10)

at the end of the while loop, to limit your FPS. Number is in milliseconds so 10ms, for example, should refresh the loop in 100FPS.

In my practice game this solution dropped CPU usage to almost 0%, without noticeable impact on responsiveness.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top