Threading With Python Curses Giving Me Weird Characters?
Solution 1:
Printing to the terminal from multiple threads will give you intermingled output like that. It is a very simple example of race condition. Use some kind of locking mechanism to coordinate writes to the terminal, or make sure to only write from one thread (for example, using a FIFO to pass message to the writing thread, which will write them to the terminal).
The weird numbers you see are part of the ANSI escape sequences that are used by programs to use special features of the terminal: writing \x1B[nF
to the output will make your terminal move the cursor one line up, for example. Curses is outputting such codes for you, and because the terminal interprets them according to the ANSI meaning, you don't usually see them. But because of the multithreading issue, those become mingled and invalid, and part of them get printed to the screen.
Solution 2:
Even if you only use curses in one thread, other processing-heavy threads can disrupt the escape sequences in the curses thread. The environment variable $ESCDELAY
indicates how long (in ms) to wait after an escape code (0x1B) is sent; and if more than that time elapsed, a ^[
keystroke (ESC) is returned by get_wch().
Post a Comment for "Threading With Python Curses Giving Me Weird Characters?"