MCPcopy
hub / github.com/amoffat/sh / output_thread

Function output_thread

src/sh/__init__.py:2730–2788  ·  view source on GitHub ↗

this function is run in a separate thread. it reads from the process's stdout stream (a streamreader), and waits for it to claim that its done

(
    log,
    stdout,
    stderr,
    timeout_event,
    is_alive,
    quit_thread,
    stop_output_event,
    output_complete,
)

Source from the content-addressed store, hash-verified

2728
2729
2730def output_thread(
2731 log,
2732 stdout,
2733 stderr,
2734 timeout_event,
2735 is_alive,
2736 quit_thread,
2737 stop_output_event,
2738 output_complete,
2739):
2740 """this function is run in a separate thread. it reads from the
2741 process's stdout stream (a streamreader), and waits for it to claim that
2742 its done"""
2743
2744 poller = Poller()
2745 if stdout is not None:
2746 poller.register_read(stdout)
2747 if stderr is not None:
2748 poller.register_read(stderr)
2749
2750 # this is our poll loop for polling stdout or stderr that is ready to
2751 # be read and processed. if one of those streamreaders indicate that it
2752 # is done altogether being read from, we remove it from our list of
2753 # things to poll. when no more things are left to poll, we leave this
2754 # loop and clean up
2755 while poller:
2756 changed = no_interrupt(poller.poll, 0.1)
2757 for f, events in changed:
2758 if events & (POLLER_EVENT_READ | POLLER_EVENT_HUP):
2759 log.debug("%r ready to be read from", f)
2760 done = f.read()
2761 if done:
2762 poller.unregister(f)
2763 elif events & POLLER_EVENT_ERROR:
2764 # for some reason, we have to just ignore streams that have had an
2765 # error. i'm not exactly sure why, but don't remove this until we
2766 # figure that out, and create a test for it
2767 pass
2768
2769 if timeout_event and timeout_event.is_set():
2770 break
2771
2772 if stop_output_event.is_set():
2773 break
2774
2775 # we need to wait until the process is guaranteed dead before closing our
2776 # outputs, otherwise SIGPIPE
2777 alive, _ = is_alive()
2778 while alive:
2779 quit_thread.wait(1)
2780 alive, _ = is_alive()
2781
2782 if stdout:
2783 stdout.close()
2784
2785 if stderr:
2786 stderr.close()
2787

Callers

nothing calls this directly

Calls 7

no_interruptFunction · 0.85
debugMethod · 0.80
readMethod · 0.80
register_readMethod · 0.45
unregisterMethod · 0.45
waitMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected