• Python 一个例子 multiprocessing.Pool.apply_async


    multiprocessing.Pool.apply_async 这个函数的用法例子,如下,

    import multiprocessing
    import multiprocessing
    import time
    import random
    import sys
    
    # print 'Testing callback:'
    def mul(a, b):
        time.sleep(0.5*random.random())
        return a * b
    
    def pow3(x):
        return x ** 3
    
    if __name__ == '__main__':
        multiprocessing.freeze_support()
    
        PROCESSES = 4
        print 'Creating pool with %d processes
    ' % PROCESSES
        pool = multiprocessing.Pool(PROCESSES)
    
    
        A = []
        B = [56, 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
    
        r = pool.apply_async(mul, (7, 8), callback=A.append)
        r.wait()
    
        r = pool.map_async(pow3, range(10), callback=A.extend)
        r.wait()
    
        if A == B:
            print '	callbacks succeeded
    '
        else:
            print '	*** callbacks failed
    		%s != %s
    ' % (A, B)
  • 相关阅读:
    html 注释和特殊字符
    html 锚点链接
    html 链接标签
    spring 利用工厂模式解耦
    html 路径
    html 图像标签
    html div和span标签
    html 文本格式化标签
    P5358 [SDOI2019]快速查询
    luoguP2679 子串
  • 原文地址:https://www.cnblogs.com/DaSunWarman/p/8042324.html
Copyright © 2020-2023  润新知