|
13 | 13 | import asyncio |
14 | 14 | import warnings |
15 | 15 |
|
16 | | -from aiohttp import ClientSession |
| 16 | +from aiohttp import ClientSession, TCPConnector |
17 | 17 | from pkg_resources import get_distribution, DistributionNotFound |
18 | 18 |
|
19 | 19 | # Obtain package version for the user-agent. Uses the installed version by |
@@ -709,27 +709,26 @@ def covidcast_nowcast( |
709 | 709 | return Epidata._request(params) |
710 | 710 |
|
711 | 711 | @staticmethod |
712 | | - def async_epidata(param_list, batch_size=100): |
| 712 | + def async_epidata(param_list, batch_size=50): |
713 | 713 | """Make asynchronous Epidata calls for a list of parameters.""" |
714 | 714 | async def async_get(params, session): |
715 | 715 | """Helper function to make Epidata GET requests.""" |
716 | 716 | async with session.get(Epidata.BASE_URL, params=params) as response: |
| 717 | + response.raise_for_status() |
717 | 718 | return await response.json(), params |
718 | 719 |
|
719 | 720 | async def async_make_calls(param_combos): |
720 | 721 | """Helper function to asynchronously make and aggregate Epidata GET requests.""" |
721 | 722 | tasks = [] |
722 | | - async with ClientSession() as session: |
| 723 | + connector = TCPConnector(limit=batch_size) |
| 724 | + async with ClientSession(connector=connector) as session: |
723 | 725 | for param in param_combos: |
724 | 726 | task = asyncio.ensure_future(async_get(param, session)) |
725 | 727 | tasks.append(task) |
726 | 728 | responses = await asyncio.gather(*tasks) |
727 | 729 | return responses |
728 | 730 |
|
729 | | - batches = [param_list[i:i+batch_size] for i in range(0, len(param_list), batch_size)] |
730 | | - responses = [] |
731 | | - for batch in batches: |
732 | | - loop = asyncio.get_event_loop() |
733 | | - future = asyncio.ensure_future(async_make_calls(batch)) |
734 | | - responses += loop.run_until_complete(future) |
| 731 | + loop = asyncio.get_event_loop() |
| 732 | + future = asyncio.ensure_future(async_make_calls(param_list)) |
| 733 | + responses = loop.run_until_complete(future) |
735 | 734 | return responses |
0 commit comments