码迷,mamicode.com
首页 > 系统相关 > 详细

多进程提速

时间:2020-10-22 22:46:37      阅读:30      评论:0      收藏:0      [点我收藏+]

标签:code   man   ==   cti   --   function   sleep   import   readline   

方式一:multiprocessing.Process

def worker(procnum, return_dict):
    ‘‘‘worker function‘‘‘
    print str(procnum) +  represent!
    return_dict[procnum] = procnum

if __name__ == __main__:
    manager = Manager()
    return_dict = manager.dict()
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=worker, args=(i,return_dict))
        jobs.append(p)
        p.start()

    for proc in jobs:
        proc.join()
    # 最后的结果是多个进程返回值的集合
    print return_dict.values()

方式二:Process

    pool = Pool(4)
    manager = Manager()
    return_dict = manager.dict()
    jobs = []
    q = Queue()

    with open(data_path, r, encoding=utf8) as fr:
        content = fr.readlines()
        for d in content[:100]:
            q.put(d)
        print(数据放入队列完毕)

    for i in range(5):
        p = Process(target=func, args=(q, return_dict))
        jobs.append(p)
        p.start()
    for proc in jobs:
        proc.join()
    print(return_dict.values())

 方式三:Pool

import multiprocessing
import time


def func(msg):
    print(hello :, msg, time.ctime())
    time.sleep(2)
    print(end, time.ctime())
    return done + msg


if __name__ == __main__:
    pool = multiprocessing.Pool(2)
    result = []
    for i in range(3):
        msg = hello %s % i
        result.append(pool.apply_async(func=func, args=(msg,)))

    pool.close()
    pool.join()

    for res in result:
        print(***:, res.get())             # get()函数得出每个返回结果的值

    print(All end--)

 

多进程提速

标签:code   man   ==   cti   --   function   sleep   import   readline   

原文地址:https://www.cnblogs.com/demo-deng/p/13857556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!