Created
February 24, 2018 04:22
-
-
Save michaelBenin/f4b2eb5c0bc387e2af4febc3b54587ac to your computer and use it in GitHub Desktop.
Bluebird's Promise.map in python
This file contains 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
'''Debugging: https://docs.python.org/3/library/asyncio-dev.html#asyncio-dev''' | |
def fetch_urls_concurrent(urls): | |
loop = asyncio.new_event_loop() | |
resp_list = [] | |
async def async_req_urls(): | |
'''Change max workers for concurrency''' | |
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: | |
futures = [] | |
for url in urls: | |
futures.append(loop.run_in_executor( | |
executor, | |
fetch_url, | |
url | |
) | |
) | |
for response in await asyncio.gather( * futures): | |
resp_list.append(response) | |
loop.run_until_complete(async_req_urls()) | |
loop.close() | |
return resp_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment