Annahme:
def get(url):
#這里是get請求
return url
for i in urls:
get(i)
#這里采用循環(huán)去調(diào)用請求
#這樣速度好像比較慢,有什么方法可以
#當多個請求時,也可以快速完成程序
Danke
學習是最好的投資!
>>> from eventor import Eventor
>>> urllist = ["https://a.com", ....]
>>> def get(url):
# 網(wǎng)絡(luò)IO耗時
return url
>>> e = Eventor(threadcount=3, taskunitcount=3, func=get, interval=1)
>>> result = e.run_with_tasklist(urllist)
>>> print(result)
自己根據(jù)需求選擇 多核多進程,還是多線程,協(xié)程加快處理。
詳情移步:https://github.com/kute/eventor
后續(xù)再 加進 async await,celery worker等
最簡單的改造就是用multiprocessing.dummy 執(zhí)行多線程任務(wù),可以查一下,設(shè)置好線程數(shù)量,然后對程序不需要太大改造,大體就能用。