Python 2.7: How to compensate for missing pool.starmap?

后端 未结 1 835
面向向阳花
面向向阳花 2020-12-19 12:33

I have defined this function

def writeonfiles(a,seed):
    random.seed(seed)

    f = open(a, \"w+\")
    for i in range(0,10):
        j = random.randint(0,         


        
相关标签:
1条回答
  • 2020-12-19 13:31

    Python 2.7 lacks the starmap pool-method from Python 3.3+ . You can overcome this by decorating your target function with a wrapper, which unpacks the argument-tuple and calls the target function:

    import os
    from multiprocessing import Pool
    import random
    from functools import wraps
    
    
    def unpack(func):
        @wraps(func)
        def wrapper(arg_tuple):
            return func(*arg_tuple)
        return wrapper
    
    @unpack
    def write_on_files(a, seed):
        random.seed(seed)
        print("%d opening file %s" % (os.getpid(), a))  # simulate
        for _ in range(10):
            j = random.randint(0, 10)
           print("%d writing %d to file %s" % (os.getpid(), j, a))  # simulate
    
    
    if __name__ == '__main__':
    
        folder = ["Test/%d.csv" % i for i in range(0, 4)]
        seed = [234124, 663123, 12345, 123833]
    
        arguments = zip(folder, seed)
    
        pool = Pool(4)
        pool.map(write_on_files, iterable=arguments)
        pool.close()
        pool.join()
    
    0 讨论(0)
提交回复
热议问题