Last active
November 23, 2018 16:52
-
-
Save pavan538/3139763a37963019043ec2bdab43d771 to your computer and use it in GitHub Desktop.
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
- This below code will get both responses in parallel. | |
import asyncio | |
import requests | |
@asyncio.coroutine | |
def main(): | |
loop = asyncio.get_event_loop() | |
future1 = loop.run_in_executor(None, requests.get, 'http://www.google.com') | |
future2 = loop.run_in_executor(None, requests.get, 'http://www.google.co.uk') | |
response1 = yield from future1 | |
response2 = yield from future2 | |
print(response1.text) | |
print(response2.text) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) | |
async def main(): | |
loop = asyncio.get_event_loop() | |
future1 = loop.run_in_executor(None, requests.get, 'http://www.google.com') | |
future2 = loop.run_in_executor(None, requests.get, 'http://www.google.co.uk') | |
response1 = await future1 | |
response2 = await future2 | |
print(response1.text) | |
print(response2.text) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment