嘿有堆棧溢出。我正在嘗試構建一個測試腳本,該腳本應該在多行(隨時間創建它們)上混合輸出不斷變化的字符(使用curses),並根據線程號創建新行。 我有下面的代碼:Python的線程詛咒給我怪異的字符?
# -*- 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
庫試過,但我與它沒有希望。線程庫至少會在瘋狂之前在幾行顯示我想要的數字。這裏是做什麼的一個例子,當我運行它:
所有我想要的是程序只是簡單地開始一個新的線程,並顯示計數到50,同時增加新的生產線做一個線一樣。我將如何去做這個?在此先感謝:)
只是一個想法,我不熟悉stdscr的東西,但在這一行stdscr.addstr(linePos,0,「testing%s ...」%i)你不叫str(i)。你有沒有試過這種方式? – Jeff
@Jeff是的,我有,它不會改變任何東西;不幸的是仍然有錯誤。 –
可能重複[curses在python getstr()刷新時](https://stackoverflow.com/questions/8569878/curses-in-python-getstr-while-refreshing) –