中断来自另一个线程的raw_input

时间:2018-11-30 10:09:04

标签: python multithreading blocking

我有两个线程:

printlock = threading.Lock()

# Main thread:
while True:
   if not(printlock.locked()):
      stdin_input = raw_input()
      do_something(stdin_input)

# Listening thread:
while True:
   socket_input = listen_to_socket()
   with printlock:
      print socket_input

现在的行为是,一旦侦听线程接收到输入,它将打印出覆盖raw_input()的内容。 raw_input的内容仍然存在,但是隐藏在其他一些输出之后。我想实现的是,当侦听线程接收到输入时,它将中断主线程的阻塞/等待raw_input完成,因此它仅在while循环的顶部恢复。

  • 如果我松开raw_input()的当前内容就可以了。
  • 解决方案仅需要在linux中工作。
  • 我要使用raw_input而不是直接从stdin读取,因为我正在使用readlines模块。

关于SO有几个相关的问题,但是没有一个真正解决我的问题-它们没有提供中断raw_input的方法。我认为应该通过信号或异常来实现,但是我还没有弄清楚该怎么做。

谢谢您的建议。

0 个答案:

没有答案