I am stuck in the output pipeline of one script to another script (both are pythons).
This question is very similar, but (1) it does not give an answer (2), I have a slight difference. So, I thought the new question would be better.
Here is the problem.
Both scenarios are almost identical:
receiver.py
import sys
import time
for line in sys.stdin:
sys.stdout.write(line)
sys.stdout.flush()
time.sleep(3)
replicator.py
import sys
import time
for line in sys.stdin:
sys.stderr.write(line)
sys.stderr.flush()
time.sleep(3)
When I execute these scripts in bash or cmd one by one, everything is fine. Both of the examples below work, and I see the input text in the output:
Work: (one line is displayed every 3 seconds)
cat data.txt | python receiver.py
cat data.txt | python replicator.py
But as soon as I go from one script to another script, they stop working:
Doesn't work: (nothing appears until the end of the file)
cat data.txt | python receiver.py | python replicator.py
, script , !
:
cat data.txt | python receiver.py | cat -n
cat data.txt | python replicator.py | cat -n
, sleep(), :
:
time.sleep(0)
:
cat data.txt | python receiver.py | python replicator.py
- , ?
. , .
UPDATE
, .
data.txt, .
receiver.py
import sys
import time
import datetime
for line in sys.stdin:
sys.stdout.write(str(datetime.datetime.now().strftime("%H:%M:%S"))+'\t')
sys.stdout.write(line)
sys.stdout.flush()
time.sleep(1)
data.txt
Line-A
Line-B
Line-C
Line-D
$> cat data.txt
Line-A
Line-B
Line-C
Line-D
$> 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
$> 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
$> cat test.log | python receiver.py | sed -e "s/^/$(date +"%H:%M:%S") /"
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
$> 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
, python script , script , . .
, (sed ), . ?