Search code examples
pythonncursestmuxcurses

How to prevent glitches appear in curses app?


I'm developing app with curses in Python. And few seconds after app starts some glitches appear randomly. Normal With glitches

First I thought it's because of tmux and $TERM variable, but it is set to screen-256. Running app outside tmux also causes this kind of glitch.

Here is the code that initializes curses screen


Solution

  • The example does this:

    import curses
    from threading import Thread
    
    
    class Screen(object):
        def __init__(self):
            self.maxx = None
            self.maxy = None
    
    self.run()
    

    implying that your application could have multiple threads writing to the screen. But curses is normally not thread-safe, so you'll end up with the threads using theirs - and other threads - values of global or static variables. It won't work well.

    If you want to use curses in a multi-threaded application, you'll have to either ensure that only one thread uses curses, or (probably) compile your own copy of the ncurses library which has rudimentary support for re-entrant code, and work within the constraints of that configuration.

    Further reading: