python多任务及返回值的处理方法
废话不多说,直接上代码!
#coding:utf-8 frommultiprocessingimportPool importtime defkeywords(title,content,top_n=5): printu'关键词提取...' printtitle,content,top_n time.sleep(3) return0,[u"晴",u"多云"] defcategory(title,content): printu'文本分类...' printtitle,content time.sleep(5) return1,[u"天气"] defrun(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))) foriteminq: r=item.get() ifr[0]==0: keywords_list=r[1] elifr[0]==1: category_list=r[1] pool.close() pool.join() returncategory_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) printtime.time()-t1
或者:
#coding:utf-8 frommultiprocessingimportPool importtime defkeywords(title,content,top_n=5): printu'关键词提取...' printtitle,content,top_n time.sleep(3) return0,[u"晴",u"多云"] defcategory(title,content): printu'文本分类...' printtitle,content time.sleep(5) return1,[u"天气"] defrun(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"]forwinq[0].get()[1]] category_list=category(title,content)[1] pool.close() pool.join() returncategory_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) printtime.time()-t1
以上这篇python多任务及返回值的处理方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。