Chinaunix首页 | 论坛 | 博客
  • 博客访问: 163325
  • 博文数量: 22
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 248
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-05 19:05
文章分类
文章存档

2018年(12)

2017年(10)

我的朋友

分类: Python/Ruby

2017-12-15 11:50:50

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\n' % 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 '\tcallbacks succeeded\n'
    else:
        print '\t*** callbacks failed\n\t\t%s != %s\n' % (A, B)
阅读(3437) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~