简体   繁体   English

我可以在python多处理中将方法传递给apply_async或map吗?

[英]Can I pass a method to apply_async or map in python multiprocessing?

How can I get the following to work? 如何获得以下服务? The main point is that I want to run a method (and not a function) asynchronously. 要点是我要异步运行方法(而不是函数)。

from multiprocessing import Pool

class Async:
    def __init__(self, pool):
        self.pool = pool
        self.run()

    def run(self):
        p.apply_async(self.f, (10, ))

    def f(self, x):
        print x*x

if __name__ == '__main__':
    p = Pool(5)
    a = Async(p)
    p.close()
    p.join()

This prints nothing. 这不会打印任何内容。

The problem appears to be due to the fact that multiprocessing needs to pickle self.f while bound methods are not picklable. 该问题似乎是由于以下事实造成的: multiprocessing需要对self.f进行酸洗,而绑定方法不可酸洗。 There is a discussion on how to solve the problem here . 对于如何解决这一问题的讨论在这里

The apply_async apparently creates an exception which is put inside the future returned. apply_async显然会创建一个异常,该异常会放在将来返回的内容中。 That's why nothing is printed. 这就是为什么什么都不打印的原因。 If a get is executed on the future, then the exception is raised. 如果将来执行get操作,则会引发异常。

Its definitely possible to thread class methods using a threadpool in python 2 - the following programme did what I would expect. 绝对有可能在python 2中使用线程池对类方法进行线程处理-以下程序实现了我所期望的。

#!/usr/bin/env python

from multiprocessing.pool import ThreadPool

class TestAsync():
  def __init__(self):
    pool = ThreadPool(processes = 2)

    async_completions = []
    for a in range(2):
      async_completions.append(pool.apply_async(self.print_int, (  a,)))

    for completion in async_completions:
      res = completion.get()
      print("res = %d" % res)

  def print_int(self, value):
    print(value)
    return (value*10)


a = TestAsync()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM