Created
May 16, 2023 18:11
-
-
Save flarco/82630d6e64f56c17510248b1e8e48e0a to your computer and use it in GitHub Desktop.
Python test_http_async.py
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 aiohttp, time | |
import asyncio, requests | |
async def fetch(session, url): | |
async with session.get(url) as response: | |
return url + '>>>> ' + await response.text() | |
async def main2(): | |
urls = [ | |
'http://python.org', | |
'https://google.com', | |
'http://yifei.me', | |
'http://python.org', | |
'https://google.com', | |
'http://yifei.me', | |
'http://python.org', | |
'https://google.com', | |
'http://yifei.me', | |
'http://python.org', | |
'https://google.com', | |
'http://yifei.me', | |
] | |
tasks = [] | |
async with aiohttp.ClientSession() as session: | |
for url in urls: | |
print(url) | |
tasks.append(fetch(session, url)) | |
# requests.get(url) | |
htmls = await asyncio.gather(*tasks) | |
for html in htmls: | |
print(html[:100]) | |
def another(): | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main2()) | |
if __name__ == '__main__': | |
time.sleep(1) | |
another() | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment