您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。