I am, unfortunately, attempting to communicate with about 14 raspberry pi's all at once (art project, burning man, don't ask...) and am using Paramiko to connect to the RPi's over SSH, then issue various other commands: sync files, start server, ect...
To do this I have been using the multiprocessing module in python, but I am running into errors. After I connect to the various RPis, I would like to have the python script hang and wait for an input such as: start server(passing the server name, location, ect), which would send the ssh command via Paramiko to start running the python script on each of the RPIs.
My question is: how do I ensure that the appropriate command gets send to the correct process/pool? For instance, if I instantiate the class which connects to the various RPis and then issue the start server command, I would like: the server on RPi_A to be initialized with the name A, the server on RPi_B to be initialized with the name B, and not RPi_A named B, ect...
Do I need to use the process command for this? Or will pools work? If so is it apply, apply_async, map, map_async. The documentation is a little vague unfortunately.
Sample Code:
import sys
import time
import paramiko
import multiprocessing as mp
login = 'pi'
password = 'raspberry'
serverIp = '192.168.1.143'
config = [
{'hostname': 'pi1.local', 'name': 'carousel'},
{'hostname': 'pi2.local', 'name': 'bench'}
]
class Dreamlandia():
def __init__(self):
pool = mp.Pool(processes=12)
results = [pool.apply_async(self.connectToServer, args=(dreamlandObject,)) for dreamlandObject in config]
output = [p.get() for p in results]
def connectToServer(self, dreamlandObject):
host = dreamlandObject['hostname']
structureName = dreamlandObject['name']
i = 1
while True:
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=login, password=password)
print ("Connected to " + structureName)
break
except paramiko.AuthenticationException:
print ("Authentication failed when connecting to " + structureName)
sys.exit(1)
except:
print ("Could not SSH to " + structureName + ", waiting for it to start")
i += 1
time.sleep(1)
# If we could not connect within time limit
if i == 30:
print ("Could not connect to " + structureName + ". Giving up")
sys.exit(1)