白云岛资源网 Design By www.pvray.com

废话不多说,直接上代码!

# 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

以上这篇python多任务及返回值的处理方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

白云岛资源网 Design By www.pvray.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
白云岛资源网 Design By www.pvray.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。