我使用python的concurrent.futuresThreadPoolExecutor启动异步任务。
按照this方法,我使用tqdm进度条监视异步调用的进度。在
我的代码如下所示: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}$
我知道我可以设置一个URL队列并控制其大小,如here所述。在
然而,我不知道如何控制吞吐量速度本身。假设我希望不超过6个URL/秒。这能用别的方法存档吗时间。睡觉(n) 在上面的例子中是target_function()?在
如何有效地控制python的ThreadPoolExecutor的吞吐量速度?在