为什么在另一个线程中使用 "getstr" 时此线程暂停? Python诅咒

Why is this thread pausing when "getstr" is used in another thread? Python Curses

我制作了一个线程,用于显示秒数的流逝。不幸的是,当我使用 curses 模块中的 getstr 时,整个脚本都会停止,包括线程。由于订单重叠,我必须使用线程锁来阻止打印出随机字符。

如果有任何关于如何解决此问题或替代方案的建议,那就太好了!

在下面的示例中,windowwindow2 已经设置...

lock = threaing.Lock()


def main_function():
  #starts thread
  t1 = threading.Thread(target=time_calc,args=(window2,))
  t1.start()
  #Gets user input
  while True:
    data = window1.addstr(y,x,"Type something in!")
    data = window1.getstr(y,x,5)

    lock.acquire()
    window1.erase()
    txt = "You said: "+data

    window1.addstr(y,x,txt)
    lock.release()


def time_calc(window2):
  current_count = 0

  while True:

    time += 1

    text = "Time Count: "+str(time)

    lock.acquire()
    window2.erase()

    window2.addstr(y,x,text)
    lock.release()

    time.sleep(1)

我的代码有问题

我发现了我的代码的问题。出于某种原因,您 不能 运行 线程内的线程,而我最初调用的 main 函数被视为线程。我想我应该在我的问题中说明这一点。抱歉

There is probably a way to run a thread in a thread, but this did not work for me.

我更新的代码

lock = threading.Lock()

def call_threads():
  t1 = threading.Thread(target=main_function,args=(window1,))
  t1.start()

  t2 = threading.Thread(target=time_calc,args=(window2,))
  t1.start()

def main_function(window1):
  #Gets user input
  while True:
    data = window1.addstr(y,x,"Type something in!")
    data = window1.getstr(y,x,5)

    lock.acquire()
    window1.erase()
    txt = "You said: "+data

    window1.addstr(y,x,txt)
    lock.release()


def time_calc(window2):
  current_count = 0

  while True:

    time += 1

    text = "Time Count: "+str(time)

    lock.acquire()
    window2.erase()

    window2.addstr(y,x,text)
    lock.release()

    time.sleep(1)

如有其他原因可能导致此错误,请评论指出!