【问题标题】:is it possible to use apply_async in instance method (or even class method's) of class that are not picklable object?是否可以在不是可腌制对象的类的实例方法(甚至是类方法)中使用 apply_async?
【发布时间】:2019-05-19 12:30:03
【问题描述】:

我想在一个类中使用来自多处理 python 库(Ubuntu 16.04 中的 python 2.7)的 apply_async, 例如,我的代码是:

class Try_multiprocess():
def multyprocese_chunks(self,chunks):
    pool = Pool(processes=2)
    site_id=564
    site_st= 564
    for chunk_ix, chunk in enumerate(chunks):
        pool.apply_async(self.execute_chunk, args=(chunk, chunk_ix, site_id, site_st,))

    print "{} wait for join".format(datetime.datetime.now())
    pool.close()
    pool.join()
    print "{} after for join".format(datetime.datetime.now())


def execute_chunk(self, chunk, chunk_ix, site_id, site_st):
    print "{} execute_chunk chunk : {} ".format(datetime.datetime.now(), chunk_ix)

这不起作用(没有打印,也没有错误)我在某处读到实例方法无法序列化,但有什么解决方法吗?也许使用静态/类方法?或任何其他方式,而不是从类中提取所有方法?

【问题讨论】:

    标签: python-2.7 multiprocessing pickle


    【解决方案1】:

    根据Python multiprocessing PicklingError: Can't pickle <type 'function'>,答案是否定的: multiprocessing 只能将 Python 对象传输到可以腌制的工作进程。如果您无法按照 unutbu 的描述重新组织您的代码, https://thelaziestprogrammer.com/python/a-multiprocessing-pool-pickle 还建议没有pickle对象的多处理方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-12
      • 2010-10-28
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多