Running many python threads and grabbing the returns values of each
Question: So i've had a look at several questions on SO, and also several webpages, and
the python pages for `thread`, `threading`, and `multiprocessing`, but i can't
seem to find anything that does what i want.

I've seen a few implementations which use queues, but they aren't really what
i want either.

Is there a way i can do something like this:

    
    
    hosts = "host1 host2 host3 host4 host5".split() #lazy but meh
    cpuInfo = {}
    
    threadPool = ThreadPool(processes=len(hosts))
    
    def getCPUUsage(host):
      proc = Popen("ssh %s.rest.of.host.name top -bin 1" % host, stdout=PIPE, shell=True)
      return proc.stdout.read()
    
    for host in hosts:
      cpuInfo[host] = threadPool.newThread(getCPUUsage(host)).getFunctionReturnStuff())
    
    threadPool.waitUntilFinished()
    
    print cpuInfo # Some dictionary with a `top -bin 1` output for each host.
    

The only example i've seen so far uses a queue to get all the info out of the
various threads by passing a `queue` as an argument to the function, and have
the function manually add it's reutnr values to said queue - or they simply do
not return anything and instead just `print` \- which is useless to me.


Answer: The following example starts one worker per CPU on the computer. For each
hostname in a list, it tells a worker to ssh to that computer, run a command,
and return the result to original caller. In my case I'm just ssh'ing once
into my laptop, but you get the idea.

    
    
    import multiprocessing, subprocess
    
    def get_value(args):
        hostname = args[0]
        return subprocess.check_output(
            'ssh {} hostname'.format(hostname),
            shell=True,
            )
    
    pool = multiprocessing.Pool()
    print list( pool.imap_unordered(
        get_value,
        [ ('localhost',) ],
        ) )
    

Example output from my lappytop:

    
    
    ['palabras\n']
    

See: <https://docs.python.org/2/library/multiprocessing.html>

