Reputation: 295
I would like to forward the output from one script to the another.
I try to do it like that:
./producer.py > ./consumer.py
Where procuder:
#!/usr/bin/env python3
import sys
import time
i=0
while True:
print(i)
i+=1
time.sleep(1)
And consumer:
#!/usr/bin/env python3
import sys
f= open("test.txt","w+")
while True:
line = sys.stdin.read()
print(line)
f.write(line)
I expect that producer will generate: 1 2 3 4 5 and this pipe will be saved in file test.txt by another script. Thank you for help.
Upvotes: 0
Views: 533
Reputation: 269
Try using subprocess and then save the data from output pipe to file. You do not need to execute the command outside.
from subprocess import PIPE,Popen
process = Popen(['python','test.py'],stderr=PIPE,stdout=PIPE)
output = process.stdout.readlines()
Upvotes: 0
Reputation: 41
Consider using a queue with two threads instead. Here is a good basic guide. Here is another guide.
Upvotes: 0
Reputation: 778
Use a pipe |
instead of >
to forward output (stdout) from one process to another (as stdin). Also, beware that the data over the pipe is buffered, which may seem like nothing is happening when there is not a lot of data and the stream is never closed.
Upvotes: 1