python怎么控制速度_如何控制python的ThreadPoolExecutor的吞吐量速度?
我使用python的concurrent.futuresThreadPoolExecutor啟動異步任務(wù)。
按照this方法,我使用tqdm進(jìn)度條監(jiān)視異步調(diào)用的進(jìn)度。在
我的代碼如下所示:with concurrent.futures.ThreadPoolExecutor(max_workers = n_jobs) as executor:
future_to_url = {executor.submit(target_function, URL): URL for URL in URL_list}
kwargs = {'total': len(future_to_url), # For tqdm
'unit': 'URL', # For tqdm
'unit_scale': True, # For tqdm
'leave': False, # For tqdm
'miniters': 50, # For tqdm
'desc': 'Scraping Progress'}
for future in tqdm(concurrent.futures.as_completed(future_to_url), **kwargs):
URL = future_to_url[future]
try:
data = future.result() # Concurrent calls
except Exception as exc:
error_handling() # Handle errors
else:
result_handling() # Handle non-errors
控制臺輸出如下所示:
^{pr2}$
我知道我可以設(shè)置一個URL隊列并控制其大小,如here所述。在
然而,我不知道如何控制吞吐量速度本身。假設(shè)我希望不超過6個URL/秒。這能用別的方法存檔嗎時間。睡覺(n) 在上面的例子中是target_function()?在
如何有效地控制python的ThreadPoolExecutor的吞吐量速度?在
總結(jié)
以上是生活随笔為你收集整理的python怎么控制速度_如何控制python的ThreadPoolExecutor的吞吐量速度?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java s结尾的工具类_Filenam
- 下一篇: python设置文件编码_python批