Skip to content Skip to sidebar Skip to footer

Running 3 Python Programs By A Single Program Via Subprocess.Popen Method

I am trying to run 3 python programs simultaneously by running a single python program I am using the following script in a separate python program sample.py Sample.py: import subp

Solution 1:

The file cannot be found because the current working directory has not been set properly. Use the argument cwd="/path/to/script" in Popen


Solution 2:

It's because your script are not in the current directory when you execute sample.py. If you three script are in the same directory than sample.py, you could use :

import os
import subprocess
DIR = os.path.dirname(os.path.realpath(__file__))

def run(script):
    url = os.path.join(DIR, script)
    subprocess.Popen([url])

map(run, ['AppFlatRent.py','AppForSale.py', 'LandForSale.py'])

But honestly, if i was you i will do it using a bash script.


Solution 3:

There might be shebang missing (#!..) in some of the scripts or executable permission is not set (chmod +x).

You could provide Python executable explicitly:

#!/usr/bin/env python
import inspect
import os
import sys
from subprocess import Popen

scripts = ['AppFlatRent.py', 'AppForSale.py', 'LandForSale.py']

def realpath(filename):
    dir = os.path.realpath(os.path.dirname(inspect.getsourcefile(realpath)))
    return os.path.join(dir, filename)

# start child processes
processes = [Popen([sys.executable or 'python', realpath(scriptname)])
             for scriptname in scripts]

# wait for processes to complete
for p in processes:
    p.wait() 

The above assumes that script names are given relative to the module.

Consider importing the modules and running corresponding functions concurently using threading, multiprocessing modules instead of running them as scripts directly.


Post a Comment for "Running 3 Python Programs By A Single Program Via Subprocess.Popen Method"