python多任务及返回值的处理方法
时间:2022-04-29 10:07:59|栏目:Python代码|点击: 次
废话不多说,直接上代码!
# coding:utf-8 from multiprocessing import Pool import time def keywords(title, content, top_n=5): print u'关键词提取...' print title, content, top_n time.sleep(3) return 0, [u"晴", u"多云"] def category(title, content): print u'文本分类...' print title, content time.sleep(5) return 1, [u"天气"] def run(title, content): keywords_list = [] category_list = [] pool = Pool(processes=2) q = [] q.append(pool.apply_async(keywords, args=(title, content, 5))) q.append(pool.apply_async(category, args=(title, content))) for item in q: r = item.get() if r[0] == 0: keywords_list = r[1] elif r[0] == 1: category_list = r[1] pool.close() pool.join() return category_list, keywords_list if __name__ == "__main__": title = u"天气预报" content = u"北京今日天气:晴转多云" t1 = time.time() category_list, keywords_list = run(title, content) print "分类结果:", " ".join(category_list) print "关键词提取结果", " ".join(keywords_list) print time.time() - t1
或者:
# coding:utf-8 from multiprocessing import Pool import time def keywords(title, content, top_n=5): print u'关键词提取...' print title, content, top_n time.sleep(3) return 0, [u"晴", u"多云"] def category(title, content): print u'文本分类...' print title, content time.sleep(5) return 1, [u"天气"] def run(title, content): keywords_list = [] category_list = [] pool = Pool(processes=2) q = [] q.append(pool.apply_async(keywords, args=(title, content, 5))) keywords_list = [w["word"] for w in q[0].get()[1]] category_list = category(title, content)[1] pool.close() pool.join() return category_list, keywords_list if __name__ == "__main__": title = u"天气预报" content = u"北京今日天气:晴转多云" t1 = time.time() category_list, keywords_list = run(title, content) print "分类结果:", " ".join(category_list) print "关键词提取结果", " ".join(keywords_list) print time.time() - t1
上一篇:详解Django+uwsgi+Nginx上线最佳实战
栏 目:Python代码
下一篇:python中yield的用法详解――最简单,最清晰的解释
本文标题:python多任务及返回值的处理方法
本文地址:http://www.codeinn.net/misctech/200463.html