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

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

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 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).

How to run two python programs on MacBook simultaneously?

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.

running command line in multiple running processes from python

I'm not sure if what I'm wanting to do is possible, but:
I have a python script (lets call it PY) that calls a batch script to start a tool in terminal mode (lets call it A). This tool gets passed a starting script (tcl script) that sets up its environment and launches a second tool (lets call it B). The two tools communicate over a TCP connection locally.
My question is, with these two programs running (A and B), can I switch back to the python script to run commands in either A or B's TCL interface?
The scripts look sort of like this:
#python PY
def ReadConigAndSetup():
#read some data
...
#run bat
subprocess.run("./some_bat.bat some_data_args")
#bat start program A and pass it a startup script
some_program_A -mode tcl -source ./some_source.tcl
#tcl some_source.tcl
setup environment
open TCP port
start program B
#program B setup tcl
some more setup
after program B has run I'd like to be able to run more commands in program B from python as parsing some of the config files is much easier in the python environment.
The answer is “it depends on the details”.
There's no reason in principle why the program being called can't work fine this way, provided the subprocess relinquishes control back (which it might or might not), but launching complex programs via a BAT file is adding an extra layer of complexity so you might want to think about whether you can simplify a bit there.
If the program running the Tcl code doesn't terminate, things get trickier. This is an area where the details are critical; Tcl code can be written to loop indefinitely — it's a programming language so of course it can be told to be annoying if you insist — and the program being controlled could also decide to loop indefinitely of its own accord, which can happen particularly with GUI applications as the looping is where the user is interacting with the GUI. On Windows, many GUI applications run disconnected from the terminal (whether they do this is a compile-time option) and waiting for them to finish can be quite annoying.
It's possible to run multiple subprocesses at once using subprocess.Popen. Be very careful if you do this. It's possible to get into deadlocks (though that depends a lot on what the subprocesses are doing). It's probably easier to just launch each subprocess from its own thread… but then you're dealing with threads and that's also complicated.

Building python Shell

I have some small python 2.6 scripts built....
Now, I would like run them as seperate processes within a python shell. Each as a seperate process. If one fails to run maybe with its timer, I would like others to continue without killing all scripts.
Should I do this as singleton gui's or combine them into bigger launch pad. My perference would be launch pad type gui....Any ideas?
Its seems that launching scripts out of SciTE, works ok.
Check joblaunch, a shell tool I made for executing interdependent jobs in parallel locally. It has more options.

Categories

Resources