使用 Python Curses 线程会给我带来奇怪的字符? [英] Threading with Python Curses giving me weird characters?
问题描述
嘿,堆栈溢出.我正在尝试构建一个测试脚本,该脚本应该在多行(随着时间的推移创建它们)上混合输出更改字符(使用curses),根据线程号创建新行.我有以下代码:
Hey there Stack Overflow. I'm trying to build a testing script that should mix outputting changing characters (using curses) on multiple lines (creating them over time), creating new lines based on the thread number. I have the below code:
# -*- coding: utf-8 -*-
import curses, time, threading
def threadedFunction(linePos):
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
try:
stdscr.clear()
for i in range(50):
stdscr.addstr(linePos, 0, "testing %s..." % i)
stdscr.refresh()
time.sleep(.1)
finally:
curses.echo()
curses.nocbreak()
curses.endwin()
pass
pass
if __name__ == "__main__":
for x in xrange(0, 4): # should produce 5 lines maximum
exec("process" + str(x) + " = threading.Thread(target = threadedFunction, args = (" + str(x) + ",))")
exec("process" + str(x) + ".start()")
我之前尝试过使用 multithreading
库,但我对它没有希望.线程库至少会在它变得疯狂之前在几行上显示我想要的数字.下面是我运行它时执行的示例:
I tried using the multithreading
library before, but I had no hope with it. The threading library at least will display the numbers I want on a few lines before it goes crazy. Here's an example of what it does when I run it:
我想要的只是让程序简单地启动一个新线程,并显示一个计数为 50 的行,同时添加新行做同样的事情.我该怎么做呢??提前致谢:)
All I want is for the program to just simply start a new thread, and display a line that counts to 50 while adding new lines doing the same thing. How would I go about doing this?? Thanks in advance :)
推荐答案
即使你只在一个线程中使用curses,其他处理繁重的线程也会破坏curses 线程中的转义序列.环境变量 $ESCDELAY
指示在发送转义码 (0x1B) 后等待多长时间(以毫秒为单位);如果超过该时间,get_wch() 将返回 ^[
击键 (ESC).
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().
这篇关于使用 Python Curses 线程会给我带来奇怪的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!