文章詳情頁
python 加快程序運行問題
瀏覽:137日期:2022-07-26 13:50:42
問題描述
假設:
def get(url): #這里是get請求 return urlfor i in urls: get(i)#這里采用循環去調用請求#這樣速度好像比較慢,有什么方法可以#當多個請求時,也可以快速完成程序
謝謝了
問題解答
回答1:>>> from eventor import Eventor>>> urllist = ['https://a.com', ....]>>> def get(url):# 網絡IO耗時return url>>> e = Eventor(threadcount=3, taskunitcount=3, func=get, interval=1)>>> result = e.run_with_tasklist(urllist)>>> print(result)
自己根據需求選擇 多核多進程,還是多線程,協程加快處理。
詳情移步:https://github.com/kute/eventor
后續再 加進 async await,celery worker等
回答2:幾個方法,并且可以結合使用
coroutine
multiprocess
multithread
celery worker
回答3:最簡單的改造就是用multiprocessing.dummy 執行多線程任務,可以查一下,設置好線程數量,然后對程序不需要太大改造,大體就能用。
相關文章:
1. bootstrp是col-md-12列的,只有col-md-10有內容,可以讓沒有內容的不占據位置嗎;2. wordpress里,這樣的目錄列表是屬于小工具還是啥?3. 常量在外面不加引號會報錯。4. mysql federated引擎無法開啟5. 百度地圖 - Android app中準備接入地圖sdk,百度VS高德哪個好一點?6. 我的怎么不顯示啊,話說有沒有QQ群什么的7. sublime text3安裝package control失敗8. python 3.4 error: Microsoft Visual C++ 10.0 is required9. android - Genymotion 微信閃退 not find plugin.location_google.GoogleProxyUI10. MySQL 使用 group by 之后然后 IFNULL(COUNT(*),0) 為什么還是會獲得 null
排行榜
