Код: Выделить всё
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
def get_parallel(arguments_list: list[dict]):
try:
with ThreadPoolExecutor(max_workers=25) as executor:
futures_buffer = [executor.submit(requests.get, **kwargs) for kwargs in arguments_list]
for future in as_completed(futures_buffer):
try:
response = future.result()
print (response.url)
yield response.url, response.status_code, response.json()['args']
except KeyboardInterrupt:
executor.shutdown(wait=False, cancel_futures=True)
yield 'KeyboardInterrupt 1'
return
except Exception as exception:
yield exception
except KeyboardInterrupt:
yield 'KeyboardInterrupt 2'
return
if __name__ == '__main__':
arguments = [dict(url=f'https://httpbin.org/get?q={i}') for i in range(200)]
for t in get_parallel(arguments):
print(t)
Подробнее здесь: https://stackoverflow.com/questions/696 ... olexecutor
Мобильная версия