vince31
Posts: 26
Joined: Thu May 23, 2013 10:26 pm
Location: Swindon or Blackpool, depends on day of the week!

running 2 python scripts together

Fri Sep 12, 2014 11:08 am

I'm new to RasPi programming and Python so please treat gently!

I have successfully created 2 Python scrips for my project that I can (and need to) run in parallel. To start them off I currently need to run the firt script/program using "sudo python Prog1.py" in the first terminal window, then open a new terminal window and type "sudo python Prog2.py" to start off my second script/program. Then everything works just fine from there. But, I really want to start my second script from the first one so that it runs automatically, then all I need to do is start the first one automatically at boot.
So my question to the experts here is: How do I (in my first Python script) start a new terminal window instance and then start up my second script from there?
I cant embed the second script in the first one as they both need to run their own separate cycle of checks/events at different rates. The first one loops every 30 seconds, cant be any shorter due to the latency required in the checks it does. The second needs to monitor inputs in near real time. so they both need to run separately. Hope there is a simple easy command to start up a new terminal in python and start my other programm, I just cant find it!

eternalfame
Posts: 7
Joined: Sun Sep 07, 2014 10:57 am

Re: running 2 python scripts together

Fri Sep 12, 2014 11:51 am

Search in Google how to use subprocess.Popen.

This should allow you to start a new process!

vince31
Posts: 26
Joined: Thu May 23, 2013 10:26 pm
Location: Swindon or Blackpool, depends on day of the week!

Re: running 2 python scripts together

Fri Sep 12, 2014 1:22 pm

Thanks for the quick reply, I've Googled as you suggested, but its a minefield of other peoples issues and complex documentation about all sorts of options and comms etc,
So is it as simple as putting the following lines at the beginning of my first python script then?

import subprocess
subprocess.Popen("./program2.py")

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: running 2 python scripts together

Fri Sep 12, 2014 1:35 pm

It's not quite what you're asking for, but an even easier solution might just be to run the first script in the background?

Code: Select all

sudo python Prog1.py & sudo python Prog2.py

gandar
Posts: 215
Joined: Sat Feb 01, 2014 5:45 pm

Re: running 2 python scripts together

Fri Sep 12, 2014 3:06 pm

I don't understand why you can't run one with the other or run both through another script. Could you explain a little more?

vince31
Posts: 26
Joined: Thu May 23, 2013 10:26 pm
Location: Swindon or Blackpool, depends on day of the week!

Re: running 2 python scripts together

Fri Sep 12, 2014 3:46 pm

To AndrewS, thanks, Ill try that simple solution first.
However, reading the Popen docs has given me ideas on how I can do it better maybe. Your solution will get me up and running while I try it out though :-)

vince31
Posts: 26
Joined: Thu May 23, 2013 10:26 pm
Location: Swindon or Blackpool, depends on day of the week!

Re: running 2 python scripts together

Fri Sep 12, 2014 4:03 pm

To Gandar, I probably can run them both from one program, but I dont know how to do that at the moment.
Let me explain, after initialising, the first prog checks 8 temp sensors in my flat, one in each room, logs the results in a data file and then acts on the results by sending commands to various wireless sockets that I have fan heaters plugged into. It runs through this cycle of checks and sending on/off commands (if required) to sockets every 30 seconds. This means I have room by room controll of my heating throug a web page interface on my phone. However, I need to monitor the web page outputs much more frequently than every 30 seconds so I have my second prog do that continuously and log any requests for on/off per room in the same data file as mentioned above. This seems to work reallywell and its been running like this for over a year now, this has saved me over £200 in electricity bills compared to last year when I had to heat the whole flat through one central thermostat using the underfloor heating it has as standard.
If you can help me (with very simple steps) get them both running together and even not using a data file to transfer data between the two progs then that would be much appreciated!

gandar
Posts: 215
Joined: Sat Feb 01, 2014 5:45 pm

Re: running 2 python scripts together

Fri Sep 12, 2014 6:39 pm

Vince,

I think there is a relatively easy solution to your problem that doesn't require much.

What version of python are you using?

Is your second program, the one that continuously checks for data updates/requests, in a continuous loop?

Lastly, do you have these programs starting automatically with the pi and if so, how are you currently doing that?

davidko
Posts: 1
Joined: Wed Sep 17, 2014 3:24 pm

Re: running 2 python scripts together

Wed Sep 17, 2014 3:39 pm

Hello!

It appears that you are using two scripts to access a single resource (the log file). You also need some level of asynchronicity (is that a word?). I believe the "correct" way to do this would be to have a single program with two threads and have the resource protected by a "mutex". You can use the Python "threading" library, something like this:

(crappy pseudocode ahead)

Code: Select all

import threading  # For Threads and Locks
import time  # For time.sleep()

class RoomHandler:
    def __init__(self, ...):
        self.logFileLock = threading.Lock()
        blah blah
    def mainTempLoop(self):
        self.logFileLock.acquire()
        blah blah, do stuff, read log file, etc
        self.logFileLock.release()
        time.sleep(30)
    def checkWebStatsLoop(self):
        self.logFileLock.acquire()
        Read/write from the log file, etc
        self.logFileLock.release()
        time.sleep(1)

if __name__ == "__main__":
    myRoomHandler = RoomHandler()
    myRoomThread = threading.Thread(target=myRoomHandler.mainTempLoop)
    myRoomThread.start() # Temperature loop function starts running w/ 30 second intervals
    myRoomHandler.checkWebStatsLoop()  # Web handler starts running in main thread
    # At this point both threads should be running indefinitely
Cheers!

vince31
Posts: 26
Joined: Thu May 23, 2013 10:26 pm
Location: Swindon or Blackpool, depends on day of the week!

Re: running 2 python scripts together

Wed Sep 17, 2014 6:23 pm

gandar wrote:Vince,

What version of python are you using? - I think I'm using ver 2.7 (how do I check?)

Is your second program, the one that continuously checks for data updates/requests, in a continuous loop? - The second Python program uses FLASK to capture the data from my web page (accessed by phone), its triggered to record the data from the log file when the web page accesses the Root URL of the Pi. I guess FLASK does this by monitoring continuously; but I certainly don't have a Python loop in it.

Lastly, do you have these programs starting automatically with the pi and if so, how are you currently doing that? - No I don't have them starting automatically at the moment, but that is the target, getting them to both run together from a single start up command in the boot file

vince31
Posts: 26
Joined: Thu May 23, 2013 10:26 pm
Location: Swindon or Blackpool, depends on day of the week!

Re: running 2 python scripts together

Wed Sep 17, 2014 6:32 pm

AndrewS wrote:It's not quite what you're asking for, but an even easier solution might just be to run the first script in the background?

Code: Select all

sudo python Prog1.py & sudo python Prog2.py
Thanks, this solution works great and its running fine right now! (not done the boot file changes to make it all auto run at boot just yet).
I guess there is a much better way of doing things though as suggested by others above using a single file with threads etc, and I will explore these options further now its up and running (thanks again for the simple fix). I hope in the end I can run both progs/threads without the need for an intermediate data file (with its constant reading and writing to the flash card) by sharing the variables between the 2 programs/threads.

vince31
Posts: 26
Joined: Thu May 23, 2013 10:26 pm
Location: Swindon or Blackpool, depends on day of the week!

Re: running 2 python scripts together

Wed Sep 17, 2014 6:41 pm

Thanks also to DavidKo, I think I understand your code. I currently just error trap the read/write actions to the "data file" and redo the action again if one or the other program cant get access because its locked out, but asynchronisity would be better (as shown in your code).
Rather than using a separate datafile can I just multi thread within one program (as you suggest) and then share the variables between the two threads? This would eliminate the need for a data file altogether and save my flash card from all the read/write actions.

One quick question though...... why do I need the following line in your code:
if __name__ == "__main__":
I ask because this line is already included in my second prog as part of the FLASK routine.

manmadha
Posts: 1
Joined: Wed Mar 14, 2018 2:09 pm

Re: running 2 python scripts together

Wed Mar 14, 2018 2:20 pm

i set up a .py file to run at boot up and i want to remove that so please tell me how to do that

gordon77
Posts: 4630
Joined: Sun Aug 05, 2012 3:12 pm

Re: running 2 python scripts together

Wed Mar 14, 2018 3:49 pm

vince31 wrote:
Wed Sep 17, 2014 6:32 pm
AndrewS wrote:It's not quite what you're asking for, but an even easier solution might just be to run the first script in the background?

Code: Select all

sudo python Prog1.py & sudo python Prog2.py
Thanks, this solution works great and its running fine right now! (not done the boot file changes to make it all auto run at boot just yet).
I guess there is a much better way of doing things though as suggested by others above using a single file with threads etc, and I will explore these options further now its up and running (thanks again for the simple fix). I hope in the end I can run both progs/threads without the need for an intermediate data file (with its constant reading and writing to the flash card) by sharing the variables between the 2 programs/threads.
If the file isn't too big, and you don't need it if the pi depowered, save it in /run/shm then it won't be using the flash card.

UR1495
Posts: 1
Joined: Mon Feb 11, 2019 6:26 am

Re: running 2 python scripts together

Mon Feb 11, 2019 6:31 am

How to stop from running this 2 python scripts together?

Return to “Python”