title: How do I pipe output of one python script to another python script
body: <p>I got stuck in piping the output of one script into another script (both are python).</p>
  <p>This <a href="https://stackoverflow.com/questions/37847232/how-do-i-pipe-output-from-one-python-script-as-input-to-another-python-script?newreg=3739a1c4d947435c8c53542aaf5180d3">question</a> is very similar but (1) it does not provide an answer (2) there is a slight difference in mine. So, I thought opening a new question would be better.</p>
  <p>Here is the problem.<br />
  Both scripts are almost identical:</p>
  <p><strong>receiver.py</strong></p>
  <pre><code>import sys
  import time
  
  for line in sys.stdin:
      sys.stdout.write(line)
      sys.stdout.flush()
      time.sleep(3)
  </code></pre>
  <p><strong>replicator.py</strong></p>
  <pre><code>import sys
  import time
  
  for line in sys.stdin:
      sys.stderr.write(line)
      sys.stderr.flush()
      time.sleep(3)
  </code></pre>
  <p>When I am executing these scripts in <em>bash</em> or <em>cmd</em> one by one, everything is fine. Both examples below are working and I see the input text in the output:</p>
  <p><strong>Works:</strong> (One line of output appears each 3 seconds)</p>
  <pre><code>cat data.txt | python receiver.py
  cat data.txt | python replicator.py
  </code></pre>
  <p>But once I pipe from one script to another script they stop working:</p>
  <p><strong>Doesn't work:</strong> (Nothing appears until the end of file is being reached)</p>
  <pre><code>cat data.txt | python receiver.py | python replicator.py
  </code></pre>
  <p>Then when I pipe the first script to another tool it works again!</p>
  <p><strong>Works:</strong></p>
  <pre><code>cat data.txt | python receiver.py | cat -n
  cat data.txt | python replicator.py | cat -n
  </code></pre>
  <p>And finally when I remove the blocking sleep() function it starts to work again:</p>
  <p><strong>Removing the timer:</strong></p>
  <pre><code>time.sleep(0)
  </code></pre>
  <p><strong>Now it works:</strong></p>
  <pre><code>cat data.txt | python receiver.py | python replicator.py
  </code></pre>
  <p>Does anybody know what is wrong with my piping?
  I am not looking for alternative ways to do it. I just want to learn what is happening here.</p>
  <h1>UPDATE</h1>
  <p>Based on the comments, I refined the examples.<br />
  Now both scripts not only print out the content of <code>data.txt</code>, but also add a time-stamp to each line.</p>
  <p><strong>receiver.py</strong></p>
  <pre><code>import sys
  import time
  import datetime
  
  for line in sys.stdin:
      sys.stdout.write(str(datetime.datetime.now().strftime(&quot;%H:%M:%S&quot;))+'\t')
      sys.stdout.write(line)
      sys.stdout.flush()
      time.sleep(1)
  </code></pre>
  <p><strong>data.txt</strong></p>
  <pre><code>Line-A
  Line-B
  Line-C
  Line-D
  </code></pre>
  <p><strong>The result</strong></p>
  <pre><code>$&gt; cat data.txt
  Line-A
  Line-B
  Line-C
  Line-D
  
  $&gt; cat data.txt | python receiver.py
  09:05:44        Line-A
  09:05:45        Line-B
  09:05:46        Line-C
  09:05:47        Line-D
  
  $&gt; cat data.txt | python receiver.py | python receiver.py
  09:05:54        09:05:50        Line-A
  09:05:55        09:05:51        Line-B
  09:05:56        09:05:52        Line-C
  09:05:57        09:05:53        Line-D
  
  $&gt; cat test.log | python receiver.py | sed -e &quot;s/^/$(date +&quot;%H:%M:%S&quot;) /&quot;
  09:17:55        09:17:55        Line-A
  09:17:55        09:17:56        Line-B
  09:17:55        09:17:57        Line-C
  09:17:55        09:17:58        Line-D
  
  $&gt; cat test.log | python receiver.py | cat | python receiver.py
  09:36:21        09:36:17        Line-A
  09:36:22        09:36:18        Line-B
  09:36:23        09:36:19        Line-C
  09:36:24        09:36:20        Line-D
  </code></pre>
  <p>As you see when I am piping the output of python script to itself, the second script waits until the first one is finished. Then it starts to digest the data.</p>
  <p>However, when I am using another tool (<code>sed</code> in this example), the tool receives the data immediately. Why it is happening?</p>