I am an beginner to python and ubuntu. I am trying to run two python scripts at the same time in terminal,my command in the terminal is:

python one.py|python three.py

one.py prints a four variable array to the terminal

three.py reads the values from terminal and stores the values to another array.

I believe this process in ubuntu terminal is called pipelining.

The problem now I am facing is the python script three.py waits until python script one.py to complete its execution. I want it to happen in real time, whenever one.py has an output, three.py should read and store this value in realtime.

output of one.py

output of one.py screenshot


code:three.py screenshot

  • If you don't want to wait I think you should look at 'named pipes' (man pipe). – muclux Apr 12 '18 at 10:27
  • 4
    Please edit your question and post your code and output as plain, code-formatted text. Screenshots of text are bad. – Byte Commander Apr 12 '18 at 10:41
  • 1) The line temp=[] seems to be useless. 2) Possible cross-site duplicate: stackoverflow.com/questions/11799300/… You should ask programming questions on Stack Overflow anyway. – Melebius Apr 12 '18 at 10:56

Your implementation of three.py is incorrect.

By writing

temp = sys.stdin.readlines()

You tell Python to read all lines from standard input and store it as list in your temp variable. It's obvious that this call blocks until the input is exhausted, which happens only when one.py is finished and exits, closing the pipeline. Only after that you start processing the input.

What you can do instead is to read from standard input line by line in a loop, like this:

#!/usr/bin/env python3
import sys
for line in sys.stdin:
  • @An0n Please refrain from posting non-useful comments and nonsense comments on posts. Thank you! – Thomas Ward Apr 16 '18 at 16:05
  • @An0n The same applies for useless edits as well, too. Knock it off. – Thomas Ward Apr 16 '18 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.