Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use threads that start and end independently of one another. This method would be useful if you knew all the commands you wanted to run ahead of time. Here is an example...</p> <pre><code>from threading import Thread import subprocess import Queue import multiprocessing class Command(object): def __init__(self, cmds): self.cmds = cmds def run_cmds(self): cmd_queue = Queue.Queue() for cmd in self.cmds: cmd_queue.put(cmd) available_threads = multiprocessing.cpu_count() for x in range(0,available_threads): t = Thread(target=self.run_cmd,args=(cmd_queue,)) t.setDaemon(True) t.start() cmd_queue.join() def run_cmd(self, cmd_queue): while True: try: cmd = cmd_queue.get() except: break print 'Thread started' process = subprocess.Popen(cmd, shell=True) process.communicate() print 'Thread finished' cmd_queue.task_done() # create list of commands you want to run cmds = ['cd /home/nater/Desktop','cd /home/nater/Desktop','cd /home/nater/Desktop','cd /home/nater/Desktop','cd /home/nater/Desktop'] # create class c = Command(cmds) # run them... c.run_cmds() </code></pre> <p>This would print....</p> <pre><code>Thread started Thread started Thread started Thread startedThread finished Thread started Thread finishedThread finished Thread finished Thread finished </code></pre> <p>As you can see from the output the subprocesses start and end independently of one another and no subprocess waits for another subprocess to finish because they are all called in different threads. Naturally, you could add timeouts and whatever else you wanted to, this is just a simple example. This assumes you know all the commands you want to run. If you wanted to add a thread timeout, see epicbrews answer. You could incorporate his thread timeout example into this one if you wanted to.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload