To avoid blocking your Event Look in Python when using asyncio, do not use `requests` but use `aiohttp`
Here is some example code. `aiohttp` gives us a session that works well on the asyncio Event Loop.
import aiohttp
import asyncio
async def fetch_real_url(session, url):
async with session.get(url) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
urls = ['http://python.org', 'http://google.com', 'http://example.com']
# Create a list of tasks
tasks = [fetch_real_url(session, url) for url in urls]
# Run them all at once
responses = await asyncio.gather(*tasks)
print(f"Downloaded {len(responses)} pages concurrently.")
if __name__ == "__main__":
asyncio.run(main())
Other useful things...
- `async` returns a coroutine object that can be scheduled.
- Calling this function returns a `future` — a placeholder for a result that hasn't happened yet. It tracks the state (Pending, Finished, Cancelled).
- The Event Loop is single-threaded. The Event Loop serves as a scheduler of these tasks.
No comments:
Post a Comment