简体   繁体   English

为什么在另一个线程中使用“getstr”时该线程会暂停? Python 诅咒

[英]Why is this thread pausing when “getstr” is used in another thread? Python Curses

I have made a thread that is supposed to show the seconds passing.我做了一个线程,应该显示经过的秒数。 Unfortunately, when I use getstr from the curses module the whole script stops, including the thread.不幸的是,当我从curses模块中使用getstr时,整个脚本都会停止,包括线程。 I have to use a thread lock to stop random characters being printed out because of overlapping orders.由于订单重叠,我必须使用线程锁来阻止随机字符被打印出来。

Any suggestions on how to fix this or an alternative would be great!有关如何解决此问题或替代方案的任何建议都会很棒!

In the below example window and window2 are already set-up...在下面的示例中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)

The problem with my code我的代码的问题

I figured out the problem with my code.我发现了我的代码的问题。 You can't run a thread inside a thread for some reason and I originally had my main function called to be considered a thread.由于某种原因,您不能在线程内运行线程,我最初将我的main function 称为线程。 I guess I should have stated this in my question.我想我应该在我的问题中说明这一点。 Sorry对不起

There is probably a way to run a thread in a thread, but this did not work for me.可能有一种方法可以在线程中运行线程,但这对我不起作用

My Updated Code我的更新代码

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)

If there is anything else that may have caused this error, please comment it!如果还有其他可能导致此错误的原因,请发表评论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM