3

I'm running a script with two threads, a thread reading from a subprocess, and the main thread.

inside the main thread, I'm reading from stdin, like so:

for line in sys.stdin:

    if proc.poll() != None:
        break

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

    <other things happen here>

(proc is the external subprocess)

Now, this works well, until I want to close my program. When the process in the other thread exits, I want the whole program to stop, but the main thread still waits for something to be entered into stdin. How can I abort that?

Basically, I want stdin to be signaled an EOF, but I have no idea how to do that.

polemon
  • 4,722
  • 3
  • 37
  • 48

1 Answers1

0

I would swap the threads around. Read from the subprocess in the main thread, and do the blocking sys.stdin stuff in the other thread. That way you can pass daemon=True to threading.Thread which makes the thread exit when the main thread exits.

eigil
  • 465
  • 10
  • 17