How to let Pool.map take a lambda function

前端 未结 4 1597
甜味超标
甜味超标 2020-12-01 07:27

I have the following function:

def copy_file(source_file, target_dir):
    pass

Now I would like to use multiprocessing to exe

4条回答
  •  一向
    一向 (楼主)
    2020-12-01 07:52

    Question is a bit old but if you are still use Python 2 my answer can be useful.

    Trick is to use part of pathos project: multiprocess fork of multiprocessing. It get rid of annoying limitation of original multiprocess.

    Installation: pip install multiprocess

    Usage:

    >>> from multiprocess import Pool
    >>> p = Pool(4)
    >>> print p.map(lambda x: (lambda y:y**2)(x) + x, xrange(10))
    [0, 2, 6, 12, 20, 30, 42, 56, 72, 90]
    

提交回复
热议问题