Skip to content

Instantly share code, notes, and snippets.

@crawftv
Last active May 22, 2019 18:10
Show Gist options
  • Save crawftv/ebe769a39d28c6163ab36175a62d8730 to your computer and use it in GitHub Desktop.
Save crawftv/ebe769a39d28c6163ab36175a62d8730 to your computer and use it in GitHub Desktop.
Gather-api-queries
async def get_data_asynchronous(min_song, max_song, song_urls):
"""
1. Establish an executor and number of workers
2. Establish the session
3. Establish the event loop
4. Create the task by list comprehensions
5. Gather tasks.
"""
with ThreadPoolExecutor(max_workers=40) as executor:
with requests.Session() as session:
loop = asyncio.get_event_loop()
tasks = [
loop.run_in_executor(
executor, request_song_info, *(session, song_num, song_urls)
)
for song_num in range(min_song, max_song)
]
for response in await asyncio.gather(*tasks):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment