Candyjet
Posts: 56
Joined: Sun Jan 10, 2016 3:45 pm

Launch 2 scripts simulaneously, simply?

Tue Jan 30, 2018 4:00 pm

Is there a simple way to call 2 python scripts to run roughly in parallel?

I want to call both python3 programs at the same time. There is no data transfer between the 2. One is a pygame display, the other flashing LED's via GPIO. I just want them to set off together. Exact parallel timing is not important.

I have had a long look at threading, and it seems v complicated for what seems, on the face of it, a simple task.

Is there an easier way?

Thanks

CJ

pcmanbob
Posts: 8386
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Launch 2 scripts simulaneously, simply?

Tue Jan 30, 2018 4:07 pm

If you want to run 2 scripts at the same time from a command line then you can simple do that like this

Code: Select all


python /home/pi/script1.py & python /home/pi/script2.py 

using the & runs the program/script in the background allowing the next to start
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Candyjet
Posts: 56
Joined: Sun Jan 10, 2016 3:45 pm

Re: Launch 2 scripts simulaneously, simply?

Tue Jan 30, 2018 4:34 pm

Thanks, pcmanbob.

I should have made it clearer that I would like to call the 2 scripts from another python script.
I have got the suggested command line method running ok.

CJ

pcmanbob
Posts: 8386
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Launch 2 scripts simulaneously, simply?

Tue Jan 30, 2018 9:20 pm

You would normally use subprocess.popen() to call one python program from another but I don't know if you can make it call 2 python programs at once, when calling one it normally waits for it to complete before retuning to the program that called it.

You might be able to do it by having a bash script to call your 2 programs containing this, and made executable

Code: Select all

#!/bin/bash
python /home/pi/script1.py & 
python /home/pi/script2.py &
and then calling the bash script from your python program using subprocess.call(the script.sh) you may need to add./ in front of the file name.

no guarantee it will work but worth a try.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”