How to run two python programs on MacBook simultaneously? - python

I was wondering if I need to run two python programs simultaneously, can I open two terminal windows and run each of them at the same time? Will this slow down the computing? If so, is there any way to quickly run several programs at the same time? Like parallel computing. Cheers.

Can I open two terminal windows and run each of them at the same time?
You can do that but they won't run parallelly.
Will this slow down the computing?
If you can run them in a truly parallel way, individual programs should not feel any slower than if they were run sequentially.
However, if you have multiple python scripts that you wish to run simultaneously in the same terminal, you can type the following lines on your bash terminal:
python3 script1.py &
python3 script2.py &
Or if you want to run them from another python script, you can do this:
# script3.py
import subprocess
subprocess.run("python3 script1.py & python3 script2.py", shell=True)
Or if you want to run multiple processes parallelly, you can take a look at python's built in multiprocessing module.

Option1: Check using GNU Screen with vertical split
Option2: Download iTerm2 for macOSX from this link.

Related

Running two instructions simultaneusly

I need to run at the same time this two instruction within a python script.
GPIO.output(17,True)
os.system('gphoto2 --capture-image-and-download')
it is important to start them simultaneously.
How can I do that?
As you know - Python is single - threaded. But you can get around this with several ways:
Run multiple Python threads. For example: split your script for to scripts and run them with to different python process simultaneously, like python script1.py in one terminal window and python script2.py in second terminal window.
Use multi-thread module which allows run Python functions in different threads. You can easily find a lot of quick tutorials or just read official docs above.
Also Python allows use multi-process too. The idea is similar like splitting your script on different scripts.

How to run 2 python scripts at sime time over ethernet?

I have 2 py scripts that are sending commands via ethernet to turn on/off logic levels in various electronic components. The GUI's themselves are a very simple interface, however, I am not a python guy. I am able to open up one GUI with Spyder, run the script and control the system just fine, but I need to run 2 scripts simultaneously. Both scripts are similar in nature and operation. Is there a way to run both in Spyder? Or one from two different IDE's? Thank you.
I'm not sure if i fully understand your use case but if the scripts don't take long to execute and when you say run simultaneously you mean at very nearly the same time then you could just write a shell script to execute them consecutively with some timer function if running on a regular schedule. Might need more details to know if that's appropriate. If you need them to run in parallel then that's a different matter.
You'll just be creating a file.sh with something along the lines of:
python script1.py
python script2.py
inside and some logic to run it on whatever schedule you desire
Do they have to run in Spyder? If they are just python scripts you should not need an IDE, just use two command line terminals and run script1.py in one, and script2.py in the other

How to create an executable on Mac to run python scripts (bash help)

Sorry if this is an easy to answer question, but I have been stressing myself out all day over this simple problem. I have never used a Macbook before, and am unfamiliar with the inner-workings. I wrote a series of six python scripts that are meant to be run in series, and have easily been accomplishing this with a batch files on my PC. However, I have been developing this program for a Mac user, and have no clue how to accomplish the same thing.
I have successfully managed to get python installed as well as all of the necessary packages, and the scripts can be run one-by-one, so the infrastructure is there.
On windows, I have been accomplishing this with the following batch script:
#echo off
python outputnotion.py
python addData.py
python listAppender.py
python inputgsheets.py
ECHO Timing out for 30 seconds to allow Google Sheets to compute values
timeout /t 30 /nobreak
python outputgsheets.py
python inputnotion.py
pause
I have no idea how to replicate this on mac, or if it's even possible. The person who will be using this code is not as familiar with python or running the scripts, so the simpler the solution the better.
Thank you so much, as I have been scratching my head all day over this seemingly simple issue.
Have you heard of the makefile? It works on UNIX-based systems, such as the MacOS. I use them mostly to store snippets of bash scripts I use frequently.
Make a file named Makefile
Within Makefile, write something like this:
scripts:
python outputnotion.py;
python addData.py;
python listAppender.py;
python inputgsheets.py;
## This is a comment
## Sleep for 30 seconds
sleep 30;
#echo Timing out for 30 seconds to allow Google Sheets to compute values;
python outputgsheets.py;
python inputnotion.py;
## Invoke a pause
read -p "Press [Enter] key to continue...";
Now, using your bash terminal in the directory where Makefile resides, type the following to execute your scripts:
$ make scripts
A shell script just runs builtin commands and external programs. You certainly can write a string of invocations to python scripts, not just compiled programs. The shell is really a full programming language, with variables, control structures and all. A bit quirky, but I've seen largeish scripts doing complex tasks (not that I'd recommend doing so, there are better tools).

Running python scrips at the same time with bash

I have 2 scripts that I need to run at the same time. One script collects data and the other plots the data live.
On PC, I can simply open 2 IDLE shells and they will run concurrently but on Mac, that isn't possible.
I wrote the following bash file as suggested in this post (Run multiple python scripts concurrently):
python script1.py &
python script2.py &
But this only runs my scripts one at a time. Is there anyway on a mac that I can get both scripts running at the same time?
You can do it all from within python by using subprocess.Popen()
import subprocess
import sys
s1 = subprocess.Popen([sys.executable, 'script1.py'])
s2 = subprocess.Popen([sys.executable, 'script2.py'])
s1.wait()
s2.wait()
For my purposes, I was able to find a workaround that's slightly more tedious. I have 2 separate bash scripts now, each containing one of the lines from the above script I initially posted. Running both the bash scripts will run both my scripts simultaneously in different shells.
As a side note, does anybody know how I can do a similar thing, where I use a single bash script to call both of the new bash scripts?
That's not true, on OS X (Mac) works as expected.
script1.py
#! /usr/bin/env python
import time
time.sleep(1)
print "script1.py"
script2.py
#! /usr/bin/env python
print "script2.py"
run
set executable permission and run in shell
./script1.py &
./script2.py &
and the output will be
script2.py
script1.py
proving that both were run concurrently (as output from second script is displayed first)

Running several programs from one program

I have 12 programs which i intend to run simultaneously. Is there any way i run all of them via one single program which when built runs the 12 programs?
I am using sublime and the programs are python.
If you just want to execute the programs one by one in a batch, you can do it in a bash script. Assuming they are executables in the same folder, you can have an .sh file with the following contents:
#!/bin/bash
python ./my_app1.py &
python ./my_app2.py &
python ./my_app3.py
If the scripts themselves have the #!/usr/bin/env python at the top to identify the interpreter, you can do chmod +x on them, and turn your runner.sh file into:
#!/bin/bash
./my_app1.py &
./my_app2.py &
./my_app3.py
On the other hand, if you want to do this from a python script, you can use:
import subprocess
import os
scripts_to_run = ['my_app1.py', 'my_app2.py', 'my_app3.py']
for s in scripts_to_run:
subprocess.Popen([os.path.join(os.getcwd(), s)])
Note 1: don't forget to include the #!/usr/bin/env python in every script on the first line.
Note 2: it is important to use subprocess.Popen() instead subprocess.call() because the latter is a blocking function that will wait for the application to finish before proceeding. With subproces.Popen() you get the concurrent execution.

Categories

Resources