Created
February 6, 2018 17:47
-
-
Save 0xDones/e5ebf6648c546899b27b94abe3fc7b06 to your computer and use it in GitHub Desktop.
Python 3 - Example of Multithreading using asyncio and concurrent
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
import requests, asyncio, concurrent | |
async def makeMultiRequests(names): | |
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: | |
loop = asyncio.get_event_loop() | |
futures = [ | |
loop.run_in_executor( | |
executor, | |
getUserId, | |
name | |
) | |
for name in names | |
] | |
return futures | |
def getUserId(username): | |
# Gets user Id | |
return userId; | |
names = ['Name 1', ..., 'Name N'] | |
loop = asyncio.get_event_loop() | |
resultList = loop.run_until_complete(makeMultiRequests(names)) | |
loop.close() | |
for result in resultList: | |
print(result.result()) # Users Id printed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment