pythonloadcursespython-curses

Simple python curses-application uses 100% CPU while running. Is this normal?


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.


Solution

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