简单多进程(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()