Reputation: 1638
I have a project based on a Raspberry Pi running the latest release of Raspbian Jessie. 4 separate Python scripts (that contain infinite loops for the operation of multiple stepper motors, other hardware devices and polling of ADCs and input devices) run concurrently in 4 separate SSH connections and communicate data between each other via a simple UDP socket server.
Everything works perfectly at this point, however, it's not feasible to have 4 separate SSH connections to the machine. Essentially, I'm looking for a way to spawn 4 separate instances of Python that:
Currently the system is based on Python 2.7. I've experimented with Python 3 (for asyncio
) to no avail. I have also, unsuccessfully, tried utilising the multiprocessing
and threading
modules.
I don't necessarily need another Python script to spawn all 4 instances; a shell script would be just fine. I understand that it may be possible using os.subprocess(shell=True)
or something along those lines?
Thanks, Adam
Upvotes: 0
Views: 349
Reputation: 31666
The bash
script starting 4 background jobs might work fine for PoC stage and testing. But if later on you would want to reliably run your code for longer than a few minutes you would need to implement some superviser logic. My advice is to use systemd
to set up 1 controller and 4 worker services instead. This way systemd
will do the heavy lifting of monitoring/restarting/etc your services and you will use ssh
just to control the master service.
Upvotes: 0
Reputation: 11425
Since this seems to be the solution:
You simply have one SSH connection to the machine that calls a single script:
ssh me@raspi "bash ~/caller.sh"
That script will call all of your functions:
#!/usr/bin/env bash
python script1.py &
python script2.py &
python script3.py &
python script4.py &
The &
at the end of the line means that the command for that line will be run in the background and bash will not wait for that line to finish before starting the next one. The &
on the last command is optional depending on whether you want to be returned to the command prompt immediately or not.
For your comment, writes to stdout are not suppressed; you will see output from all 4 python scripts intermingled based on how far through each script it is.
Upvotes: 2