Skip to content

Instantly share code, notes, and snippets.

@flarco
Created May 16, 2023 18:11
Show Gist options
  • Save flarco/82630d6e64f56c17510248b1e8e48e0a to your computer and use it in GitHub Desktop.
Save flarco/82630d6e64f56c17510248b1e8e48e0a to your computer and use it in GitHub Desktop.
Python test_http_async.py
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