python 多线程编程

来源:互联网 发布:局域网网络扫描仪 编辑:程序博客网 时间:2024/06/15 17:04

参考网址:http://www.cnblogs.com/tkqasn/p/5701230.html

#实例1:#-*- coding:utf-8 -*-from multiprocessing import Poolfrom math import hypotfrom random import randomimport timedef f(x):  return x[1]*x[0] #map映射多参数if __name__ == '__main__':    p = Pool(12)    print( p.map(f, [(1,2), (2,3), (4,6)]) )
实例2:#-*- coding:utf-8 -*-from multiprocessing import Poolfrom featureProject.wyl_features import make_train_setimport timedef fun(x):    temp_data, temp_labels = make_train_set(x, x + 1000000)     temp_data, temp_labels = None, Noneif __name__ == '__main__':    t_start = time.time()    pool = Pool(8)    args = [ i for i in range(18000000,30000001,1000000) ]    pool.map(fun, args)    pool.close();     pool.join()  # 进程池中进程执行完毕后再关闭,如果注释,那么程序直接关闭。    pool.terminate()    print 'the program time is :%s'%( time.time() - t_start )
原创粉丝点击