0

I have two scripts in Python.

sub.py code:

import time
import subprocess as sub

while 1:
  value=input("Input some text or number") # it is example, and I don't care about if it is number-input or text-raw_input, just input something
  proces=sub.Popen(['sudo', 'python', '/home/pi/second.py'],stdin=sub.PIPE)
  proces.stdin.write(value)

second.py code:

import sys
while 1:
 from_sub=sys.stdin()#or sys.stdout() I dont remember...
 list_args.append(from_sub) # I dont know if syntax is ok, but it doesn't matter
 for i in list_arg:
    print i

First I execute sub.py, and I input something, then second.py file will execute and printing everything what I inputed and again and again...
The thing is I don't want to open new process. There should be only one process. Is it possible?

Give me your hand :)

Magnus2005
  • 97
  • 3
  • 13
  • Better to use python naming conventions,```while True``` instead of ```while 1``` – navyad Mar 14 '16 at 08:06
  • In Python 2.x it's faster to use `while 1` instead of `while True` as `True` isn't a keyword but a global that needs to be interpreted, so if you need maximum speed `while 1` beats `while True`. In Python 3.x `True|False` became keywords so the byte code generated is equivalent. – Christian Witts Mar 14 '16 at 08:20
  • Your code is not valid Python, indetion uses different numbers of spaces. –  Mar 14 '16 at 11:17

1 Answers1

1

This problem can be solved by using Pexpect. Check my answer over here. It solves a similar problem

https://stackoverflow.com/a/35864170/5134525.

Another way to do that is to use Popen from subprocess module and setting stdin and stdout as pipe. Modifying your code a tad bit can give you the desired results

from subprocess import Popen, PIPE
#part which should be outside loop
args = ['sudo', 'python', '/home/pi/second.py']
process = Popen(args, stdin=PIPE, stdout=PIPE)
while True:
    value=input("Input some text or number")
    process.stdin.write(value)

You need to open the process outside the loop for this to work. A similar issue is addressed here in case you want to check that Keep a subprocess alive and keep giving it commands? Python

This approach will lead to error if child process quits after first iteration and close all the pipes. You somehow need to block the child process to accept more input. This you can do by either using threads or by using the first option i.e. Pexpect

Community
  • 1
  • 1
Sharad
  • 1,867
  • 14
  • 33
  • [_italic_ ' process.stdin.write(value) '] can't be inside loop... Error 22. – Magnus2005 Mar 14 '16 at 09:58
  • but look, child process can't finish because there is while 1: its mean it is always true and process should be always runed after execute once. Am I right? – Magnus2005 Mar 14 '16 at 15:01
  • can you include complete piece of code along with the input you are giving ? – Sharad Mar 14 '16 at 15:32
  • As far as I know; as soon as the end of file is encountered i.e. all your arguments are read the first time; the pipe will close. That's how pipes are by design. The other methods to do this is using threads or sockets. Pexpect is a good option – Sharad Mar 14 '16 at 15:43