I’m writing a script in Python to ssh into a few computers (about ten) and have them start rendering 3d images from Blender. It works fine except the next computers’s renders won’t start until the previous ones are finished. Is there a way to start the commands and have them all run concurrently on their own machines?
what my code looks like:
import os
path = /home/me
comp1 = ['sneffels','1','2'] #computer name, start frame, end frame
comp2 = ['bierstadt','3','4']
comp3 = ['diente','5','6']
os.system("ssh igp@" + str(comp1[0]) + " blender -b "+ str(path) +" -s " + str(comp1[1]) + " -e " + str(comp1[2]) + " -a")
os.system("ssh igp@" + str(comp2[0]) + " blender -b "+ str(path) +" -s " + str(comp2[1]) + " -e " + str(comp2[2]) + " -a")
os.system("ssh igp@" + str(comp3[0]) + " blender -b "+ str(path) +" -s " + str(comp3[1]) + " -e " + str(comp3[2]) + " -a")
The problem is that
os.systemdoesn’t return until the program is done, andsshisn’t done until the command you gave it is done.This is one of many reasons not to use
os.system—as the documentation explicitly says:In
subprocess, you can create a bunch of subprocesses, and then join them all after they’ve all been kicked off. For example:That probably isn’t the best way to do this. Read the subprocess docs to understand why
shell=Trueand passing a string is usually not as good as passing alistof parameters, other ways to manage your subprocesses, etc.. But meanwhile, this is probably the simplest change from what you already have.Another alternative is to not shell out to the
sshcommand in the first place, but instead use something likeparamikoto spawn the remote processes from within Python.