【问题标题】:How to abort waiting for sys.stdin in python3?如何中止在 python3 中等待 sys.stdin?
【发布时间】:2016-11-20 06:11:24
【问题描述】:

我正在运行一个包含两个线程的脚本,一个从子进程读取的线程和一个主线程。

在主线程中,我正在阅读stdin,如下所示:

for line in sys.stdin:

    if proc.poll() != None:
        break

    proc.stdin.write(line)
    proc.stdin.flush()

    <other things happen here>

proc 是外部子进程)

现在,这很好,直到我想关闭我的程序。当另一个线程中的进程退出时,我希望整个程序停止,但是主线程仍然在等待输入东西到stdin。我怎样才能中止它?

基本上,我希望stdin 收到EOF 的信号,但我不知道该怎么做。

【问题讨论】:

    标签: python-3.x stdin sys


    【解决方案1】:

    我会交换线程。从主线程中的子进程读取,并在另一个线程中执行阻塞sys.stdin 的东西。这样您就可以将daemon=True 传递给threading.Thread,这会使主线程退出时线程退出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多