您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章给大家分享的是有关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
以上就是python中怎么处理多任务和返回值,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。