运行多个python线程并获取每个线程的返回值



所以我已经看了几个关于So的问题,还有几个网页,以及thread, threadingmultiprocessing的python页面,但我似乎找不到任何我想要的。

我见过一些使用队列的实现,但它们也不是我真正想要的。

有没有办法我可以这样做:

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.

到目前为止,我所见过的唯一示例使用队列通过传递queue作为函数的参数来获取各种线程的所有信息,并让函数手动添加它的reutnr值到所述队列-或者它们根本不返回任何东西,而只是print -这对我来说是无用的。

下面的示例在计算机上的每个CPU上启动一个worker。对于列表中的每个主机名,它告诉worker ssh到该计算机,运行命令,并将结果返回给原始调用者。在我的情况下,我只是在我的笔记本电脑上ssh一次,但是你知道的。

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',) ],
    ) )

笔记本电脑输出示例:

['palabrasn']

见:https://docs.python.org/2/library/multiprocessing.html

正如我在评论中提到的,我会这样做:

class MessagingThread(threading.Thread):
    def __init__(self, queue=None, **kwds):
        super(MessagingThread, self).__init__(**kwds)
        self.value = None
        self.queue = queue
    def run(self):
        """Method representing the thread's activity.
        You may override this method in a subclass. The standard run() method
        invokes the callable object passed to the object's constructor as the
        target argument, if any, with sequential and keyword arguments taken
        from the args and kwargs arguments, respectively.
        """
        try:
            if self._target:
                self.value = self._target(*self._args, **self.kwargs)
                self.queue.put(self.value)
        finally:
            # Avoid a refcycle if the thread is running a function with
            # an argument that has a member that points to the thread.
            del self._target, self._args, self._kwargs

相关内容

  • 没有找到相关文章

最新更新