How to launch a couple of python scripts from a first python script and then terminate them all at once?
Question: I have a function in a python script which should launch another python script
multiple times, I am assuming this can be done like this(Script is just my
imagination of how this would work.)

    
    
    iterations = input("Enter the number of processes to run")
    for x in range(0, iterations):
        subprocess.call("python3 /path/to/the/script.py", shell=True)
    

but, I also need to pass over some defined variables into the other script,
for example, if

    
    
    x = 1
    

in the first script, then, I need x to have the same value in the second
script without defining it there, I have NO idea how to do that.

And then also killing them, I have read about some method using PIDs, but
don't those change every time?

Most of the methods I found on Google looked overly complex and what I want to
do is really simple. Can anyone guide me in the right direction as to what to
use and how I should go at accomplishing it?


Answer: **I have a function in a python script which should launch another python
script multiple times, I am assuming this can be done like this(Script is just
my imagination of how this would work.)**

** Here is the subprocess manual page which contains everything I will be
talking about <https://docs.python.org/2/library/subprocess.html>

One of the way to call one script from other is using subprocess.Popen
something on the lines

    
    
       import subprocess
       for i in range(0,100):
           ret = subprocess.Popen("python3 /path/to/the/script.py",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
    

you can use the return value from Open to make the call synchronous using the
communicate method.

    
    
      out,err = ret.communicate()
    

This would block the calling script until the subprocess finishes.

**I also need to pass over some defined variables into the other script??**

There are multiple ways to do this. 1\. Pass parameters to the called script
and parse it using OptionPraser or `sys.args` in the called script have
something like

    
    
         from optparse import OptionParser
         parser = OptionParser()
         parser.add_option("-x","--variable",action="store_true",dest="xvalue",default=False)
         (options,args) = parser.parse_args()
    
         if options.xvalue == True:
              ###do something
    

in the callee script use subprocess as

    
    
         ret = subprocess.Popen("python3 /path/to/the/script.py -x",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
    

Note the addition of -x parameter

  2. You can use args parse

<https://docs.python.org/2/library/argparse.html#module-argparse>

  3. Pass the subprocess a environment variable which can be used to configure the subprocess. This is fast but this only works one way, i.e. from parent process to child process. in called script
    
         import os
     x = int(os.enviorn('xvalue'))
    

in callee script set the environment variable

    
    
        import os
        int x = 1
        os.environ['xvalue'] = str(x)
    

  4. Use sockets or pipes or some other IPC method

**And then also killing them, I have read about some method using PIDs, but
don't those change every time?**

again you can use subprocess to hold the process id and terminate it this will
give you the process id

    
    
    ret.pid
    

you can then use .terminate to terminate the process if it is running

    
    
    ret.terminate()
    

to check if the process is running you can use the poll method from subprocess
Popen. I would suggest you to check before you terminate the process

    
    
    ret.poll()
    

poll will return a None if the process is running

