简单多进程(python3)

import time
from multiprocessing import Pool
from tqdm import tqdm
# 简单多进程


def hello_world(name):
    time.sleep(1)
    sentence = "hello world " + name
    return '{}'.format(sentence)


def main():
    num_processor = 10
    p = Pool(num_processor)
    names = open('names.txt').readlines()
    fw = open('results.txt', 'w+')
    for result in tqdm(p.imap(hello_world, names), total=len(names)):
        fw.write(result + '\n')
    fw.close()


if __name__ == '__main__':
    main()

 

posted @ 2019-04-13 17:38  Adamanter  阅读(159)  评论(0编辑  收藏  举报