4

This is my main python script:

import time
import subprocess
def main():
   while(True):
       a=input("Please enter parameter to pass to subprocess:")
       subprocess.Popen(args="python child.py")
       print(f"{a} was started")
       time.sleep(5)
if __name__ == '__main__':
    main()

This is python child script named child.py:

def main(a):
    while(True):
        print(a)

if __name__ == '__main__':
    main(a)

How to pass value to argument a which is in the child subprocess?

Artiom Kozyrev
  • 3,526
  • 2
  • 13
  • 31
  • 1
    If you want to continuously pass a value to a child process: you can use pipes like @Poolka has done or use a socket to write and read data. – bhathiya-perera Sep 24 '18 at 16:21

3 Answers3

2

You need to use command line arguments, like this;

import time
import subprocess

def main():
   while(True):
       a=input("Please enter parameter to pass to subprocess:")
       subprocess.Popen(["python", "child.py", a])
       print(f"{a} was started")
       time.sleep(5)

if __name__ == '__main__':
    main()

child.py:

import sys

def main(a):
    while(True):
        print(a)

if __name__ == '__main__':
    a = sys.argv[1]
    main(a)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
ruohola
  • 21,987
  • 6
  • 62
  • 97
2

You may use subprocess.PIPE to pass data between your main process and spawned subprocess.

Main script:

import subprocess


def main():
    for idx in range(3):
        a = input(
            'Please enter parameter to pass to subprocess ({}/{}): '
            .format(idx + 1, 3))
        print('Child in progress...')
        pipe = subprocess.Popen(
            args='python child.py',
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE)
        pipe.stdin.write(str(a).encode('UTF-8'))
        pipe.stdin.close()
        print('Child output is:')
        print(pipe.stdout.read().decode('UTF-8'))


if __name__ == '__main__':
    main()

Child script:

import sys
import time


def main(a):
    for dummy in range(3):
        time.sleep(.1)
        print(a)


if __name__ == '__main__':
    a = sys.stdin.read()
    main(a)

Output:

>>> python main.py
Please enter parameter to pass to subprocess (1/3): qwe
Child in progress...
Child output is:
qwe
qwe
qwe

Please enter parameter to pass to subprocess (2/3): qweqwe
Child in progress...
Child output is:
qweqwe
qweqwe
qweqwe

Please enter parameter to pass to subprocess (3/3): 123
Child in progress...
Child output is:
123
123
123
  • 1
    This is the correct answer if you want to continuously pass some value to a child. – bhathiya-perera Sep 24 '18 at 16:17
  • Hi @Poolka thank you very much - all works fine, could you tell me where you found information about the command pipe.stdin.write(str(a).encode('UTF-8')) ? I read https://docs.python.org/3/library/subprocess.html but found no information about the command there. Actually it seems to me that I do not learn from standard library the write way since I often do not get the needfull information from it. – Artiom Kozyrev Sep 25 '18 at 10:20
  • 1
    @ArtiomKozyrev I learned this from `Programming in Python 3: Complete Introduction` by Mark Summerfield. The book is in English and a little bit outdated (it is about Python 3.1) but it is a really `complete` introduction (not deep but very wide). I guess (while not sure) there are more fresh alternatives to this book. –  Sep 25 '18 at 11:09
  • 1
    @ArtiomKozyrev `The Python 3 Standard Library by Example` by Doug Hellmann seems to have pretty wide coverage of Python 3 standard library potential. Just opinion based on the table of contents. Not sure if it is available for free. –  Sep 25 '18 at 11:22
2

The easiest way to pass arguments to a child process is to use command line parameters.

The first step is to rewrite child.py so that it accepts command line arguments. There is detailed information about parsing command line arguments in this question: How to read/process command line arguments? For this simple example though, we will simply access the command line arguments through sys.argv.

import sys

def main(a):
    while(True):
        print(a)

if __name__ == '__main__':
    # the first element in the sys.argv list is the name of our program ("child.py"), so
    # the argument the parent process sends to the child process will be the 2nd element
    a = sys.argv[1]
    main(a)

Now child.py can be started with an argument like python child.py foobar and the text "foobar" will be used as the value for the a variable.

With that out of the way, all that's left is to rewrite parent.py and make it pass an argument to child.py. The recommended way to pass arguments with subprocess.Popen is as a list of strings, so we'll do just that:

import time
import subprocess

def main():
   while(True):
       a = input("Please enter parameter to pass to subprocess:")
       subprocess.Popen(["python", "child.py", a])  # pass a as an argument
       print(f"{a} was started")
       time.sleep(5)

if __name__ == '__main__':
    main()
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149