Skip to content

Instantly share code, notes, and snippets.

@0xDones
Created February 6, 2018 17:47
Show Gist options
  • Save 0xDones/e5ebf6648c546899b27b94abe3fc7b06 to your computer and use it in GitHub Desktop.
Save 0xDones/e5ebf6648c546899b27b94abe3fc7b06 to your computer and use it in GitHub Desktop.
Python 3 - Example of Multithreading using asyncio and concurrent
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