簡體   English   中英

為什么在另一個線程中使用“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 function 稱為線程。 我想我應該在我的問題中說明這一點。 對不起

可能有一種方法可以在線程中運行線程,但這對我不起作用

我的更新代碼

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)

如果還有其他可能導致此錯誤的原因,請發表評論!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM