1

I have two python scripts that use two different cameras for a project I am working on and I am trying to run them both inside a different script or within each other, either way is fine.

import os

os.system('python 1.py')

os.system('python 2.py')

My problem however is that they don't run at the same time, I have to quit the first one for the next to open. I also tried doing it with bash as well with the & shell operator

python 1.py &
python 2.py &

And this does in fact make them both run however the issue is that they both run endlessly in the background and I need to close them rather easily. Any suggestion what I can do to avoid the issues with these implementations

1
  • Because your python scripts are programs that never ends until user doesn't decide to close them or error raises your first statement blocks your calling program. So until you don't close python1.py obviously python will never go to execute the next statement that in your case is another endless program python2.py. For solving this you need to create two new process like when forking a child (Look on processes concepts) and communicate with the in some way. Commented Nov 23, 2018 at 19:41

3 Answers 3

2

You could do it with multiprocessing

import os
import time
import psutil
from multiprocessing import Process

def run_program(cmd):
    # Function that processes will run
    os.system(cmd)

# Initiating Processes with desired arguments
program1 = Process(target=run_program, args=('python 1.py',))
program2 = Process(target=run_program, args=('python 2.py',))

# Start our processes simultaneously
program1.start()
program2.start()

def kill(proc_pid):
    process = psutil.Process(proc_pid)
    for proc in process.children(recursive=True):
        proc.kill()
    process.kill()

# Wait 5 seconds and kill first program
time.sleep(5)
kill(program1.pid)
program1.join()

# Wait another 1 second and kill second program
time.sleep(1)
kill(program2.pid)
program2.join()

# Print current status of our programs
print('1.py alive status: {}'.format(program1.is_alive()))
print('2.py alive status: {}'.format(program2.is_alive()))
Sign up to request clarification or add additional context in comments.

5 Comments

I don't think he wants to terminate the called scripts after some fixed time. It's more like execute this two scripts and wait for when they're closed so check last conditions and exit. So you need to wait for both of them tho terminate independently in which order the scripts are closed.
He said that they run forever and he needs to be able to close them they both run endlessly in the background and I need to close them rather easily, so that's what my code do. If however you'd like to wait for them to finish just remove lines with terminate() function and keep join() that will wait for processes to finish.
I tried the current code and it launches both programs but its actually not terminating them with the timer
I tested it with python programs that run infinite loop and had same problem. I fixed it using psutil library, you can try code now.
Works like a charm
0

One possible method is to use systemd to control your process (i.e. treat them as daemons).

This is how I control my Python servers since they need to run in the background and be completely detached from the current tty so I can exit my connection to the machine and the continue processes continue. You can then also stop the server later using systemctl, as explained below.

Instructions:

Create a .service file and save it in /etc/systemd/system, with contents along the lines of:

[Unit]
Description=daemon one

[Service]
ExecStart=/path/to/1.py

and repeat with one going to 2.py.

Then you can use systemctl to control your daemons.

First reload all config files with:

systemctl daemon-reload

then start either of your daemons (where my_daemon.service is one of your unit files):

systemctl start my_daemon

it should now be running and you should find it in:

systemctl list-units

You can also check its status with:

systemctl status my_daemon

and stop/restart them with:

systemctl stop|restart my_daemon

Comments

0

Use subprocess.Popen. This will create a child process and return its pid.

pid = Popen("python 1.py").pid

And then check out these functions for communicating with the child process and checking if it is still running.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.