Last active
May 22, 2019 18:10
-
-
Save crawftv/ebe769a39d28c6163ab36175a62d8730 to your computer and use it in GitHub Desktop.
Gather-api-queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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