User avatar
sapnho
Posts: 244
Joined: Sun Sep 14, 2014 3:49 pm
Location: Frankfurt am Main
Contact: Website

Execute script within script and control it

Tue Dec 30, 2014 1:22 pm

I would like to start a python script from within a master python script BUT:

- it should not stop the master script but just execute alongside
AND
- I need to be able to terminate the execution of that script at any time.

I am using the subprocess.check_output command for an os status request but I guess this question refers to something completely different. How would I tackle that problem? :roll:
www.TheDigitalPictureFrame.com - Build a really great digital picture frame with the Raspberry Pi.
With lots of great tips from this forum.
Please submit your digital picture frame project to the user gallery here: bit.ly/2WkyjW1

User avatar
paddyg
Posts: 2561
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Execute script within script and control it

Tue Dec 30, 2014 2:51 pm

@sapnho, you can do this if you open the process with pipes to and from it. You can make your child program respond to a shutdown instructions. Alternatively (or as well) you can use subprocess to kill child processes. This subject has cropped up here a couple of times so you might be able to search this forum using appropriate words!
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
sapnho
Posts: 244
Joined: Sun Sep 14, 2014 3:49 pm
Location: Frankfurt am Main
Contact: Website

Re: Execute script within script and control it

Tue Dec 30, 2014 3:05 pm

Thanks Paddy, this should give me the right keywords to search. Which of your proposed alternatives would you recommend (Python 2.7)?
www.TheDigitalPictureFrame.com - Build a really great digital picture frame with the Raspberry Pi.
With lots of great tips from this forum.
Please submit your digital picture frame project to the user gallery here: bit.ly/2WkyjW1

User avatar
paddyg
Posts: 2561
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Execute script within script and control it

Tue Dec 30, 2014 5:13 pm

I realise that there is an example of how to use pipes with subprocess in the pi3d_demos/JukeBox.py which you have already downloaded, so you can study that at leisure!

It depends a) how much it matters that the child process shuts down in an orderly fashion b) if it's something you have inherited or have written yourself so can add something to allow you to stop it
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
sapnho
Posts: 244
Joined: Sun Sep 14, 2014 3:49 pm
Location: Frankfurt am Main
Contact: Website

Re: Execute script within script and control it

Tue Dec 30, 2014 5:51 pm

I am not sure if I fully understood the code snippets in your jukebox file. This is where I am right now:
My master file is:

Code: Select all

#!usr/bin/python

import time
import subprocess
import psutil

print "Calling child process now"
subprocess.Popen("python /home/pi/frame/slave.py", shell=True)
time.sleep(70)
#kill child process after 70 seconds

print "End of master process"
My slave file is:

Code: Select all

#!usr/bin/python

import time

while True:

    print "Slave - a message every 10 seconds"
    time.sleep(10) 
So basically what I want to do, is invoke the slave process and let it run for 70 seconds.
So, I am looking for the command to terminate the child process. Also, I am looking for a way to check if the child process is running.
www.TheDigitalPictureFrame.com - Build a really great digital picture frame with the Raspberry Pi.
With lots of great tips from this forum.
Please submit your digital picture frame project to the user gallery here: bit.ly/2WkyjW1

User avatar
sapnho
Posts: 244
Joined: Sun Sep 14, 2014 3:49 pm
Location: Frankfurt am Main
Contact: Website

Re: Execute script within script and control it

Tue Dec 30, 2014 8:10 pm

Paddy, to give you some more background what I am looking for: Instead of using cron, I would like to launch PictureFrame.py based on a number of parameters (geofencing - who is in the house etc., calender, sunset, email commands etc.) to control the picture frame.
So, my plan is to have a master control script that checks various things constantly and launches & terminates PictureFrame.py.
www.TheDigitalPictureFrame.com - Build a really great digital picture frame with the Raspberry Pi.
With lots of great tips from this forum.
Please submit your digital picture frame project to the user gallery here: bit.ly/2WkyjW1

User avatar
paddyg
Posts: 2561
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Execute script within script and control it

Wed Dec 31, 2014 1:34 am

Hi, sorry not to reply sooner, lots of visitors! If you just want to stop it you can just do

Code: Select all

#!usr/bin/python

import time
import subprocess
import psutil

print "Calling child process now"
p = subprocess.Popen(['python', '/home/pi/frame/slave.py'])
time.sleep(70)
#kill child process after 70 seconds
p.kill()

print "End of master process"
In theory you can pass info to the picture frame to change its behaviour without stopping it by using the pipe technique but this might prove more complicated (definitely would but may be nicer!) You would have to run an additional thread in the picture frame that was constantly listening for input. Something like

Code: Select all

run_flag = True

def check_input():
  global run_flag
  while True:
    inp = input('what to do?')
    if inp == b'STOP':
      run_flag = False

t = threading.Thread(target=check_input)
t.daemon = True
t.start()
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
sapnho
Posts: 244
Joined: Sun Sep 14, 2014 3:49 pm
Location: Frankfurt am Main
Contact: Website

Re: Execute script within script and control it

Wed Dec 31, 2014 7:14 am

Thanks so much, Paddy.

I had tried the

Code: Select all

p.kill()
command earlier buy because my

Code: Select all

p = subprocess.Popen(['python', '/home/pi/frame/slave.py'])
syntax was incorrect, the slave kept running like a limping zombie refusing to die. Works great now!

What would be the advantage of your second solution? Is child script suicide preferable over blunt beheading by the parent script? (Programming has a pretty brutal language if you ask me.. :shock: )
www.TheDigitalPictureFrame.com - Build a really great digital picture frame with the Raspberry Pi.
With lots of great tips from this forum.
Please submit your digital picture frame project to the user gallery here: bit.ly/2WkyjW1

User avatar
sapnho
Posts: 244
Joined: Sun Sep 14, 2014 3:49 pm
Location: Frankfurt am Main
Contact: Website

Re: Execute script within script and control it

Wed Dec 31, 2014 7:16 am

Ah, and one more question: Before starting a subprocess, I would like to check if the subprocess may already be running.
How would I do that?
www.TheDigitalPictureFrame.com - Build a really great digital picture frame with the Raspberry Pi.
With lots of great tips from this forum.
Please submit your digital picture frame project to the user gallery here: bit.ly/2WkyjW1

User avatar
paddyg
Posts: 2561
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Execute script within script and control it

Thu Jan 01, 2015 2:45 am

If you always start the subprocess from one program then it should be running if p still exists and should be out of the way after running p.kill() You shouldn't be wanting to start multiple instances so you could do an if p: p.kill() inside a try/except
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Execute script within script and control it

Thu Jan 01, 2015 3:55 am

sapnho wrote:I would like to start a python script from within a master python script BUT:

- it should not stop the master script but just execute alongside
AND
- I need to be able to terminate the execution of that script at any time.

I am using the subprocess.check_output command for an os status request but I guess this question refers to something completely different. How would I tackle that problem? :roll:
What you want to do is architecturally incorrect.

In a case like this Google is your friend.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
sapnho
Posts: 244
Joined: Sun Sep 14, 2014 3:49 pm
Location: Frankfurt am Main
Contact: Website

Re: Execute script within script and control it

Thu Jan 01, 2015 4:42 pm

Actually, I don't want to run the same subprocess twice either. The checking would be just to avoid that from happening.

In my case I found, I can use the state of the HDMI output to determine if the process is running.
www.TheDigitalPictureFrame.com - Build a really great digital picture frame with the Raspberry Pi.
With lots of great tips from this forum.
Please submit your digital picture frame project to the user gallery here: bit.ly/2WkyjW1

User avatar
sapnho
Posts: 244
Joined: Sun Sep 14, 2014 3:49 pm
Location: Frankfurt am Main
Contact: Website

Re: Execute script within script and control it

Fri Jan 02, 2015 7:30 pm

Hi Paddy, everything works fine now!
I have summarized my findings here: https://tathros.squarespace.com/blog/20 ... yes-we-can
Thanks a lot for your help!
www.TheDigitalPictureFrame.com - Build a really great digital picture frame with the Raspberry Pi.
With lots of great tips from this forum.
Please submit your digital picture frame project to the user gallery here: bit.ly/2WkyjW1

Return to “Python”