I am currently studying asynchronous processing of python.
So I'm thinking of doing web scraping using asyncio, but I can't honestly understand the behavior of the program.
I would like to write a question based on the program below.
Would you please explain if you like?
import asyncio from aiohttp import ClientSession async def hello (url): async with ClientSession () as session: async with session.get (url) as response: response = await response.read () print (response) loop = asyncio.get_event_loop () loop.run_until_complete (hello ("http://httpbin.org/headers"))
I think that the function with async at the beginning is processed asynchronously.
async with ClientSession () as session:
async with session.get (url) as response:
I'm not sure how it works asynchronously.
When making await, I feel that the function with async is not called, so
Is it the idea that async is attached to a module that supports asynchronization from the outside (ClientSession etc.) and it is executed by await?
Sorry for the very confusing question. I looked around various sites, but I couldn't grasp it well, so I asked a question.
Answer # 1
Below is the code that was rewritten synchronously.
from requests import Session def hello (url): with Session () as session: with session.get (url) as response: response = response.raw.read () print (response) hello ("http://httpbin.org/headers")
with XYZ:In Case of
XYZ .__ enter__ ()When
XYZ .__ exit__ ()Is called. On the other hand
async with XYZ:In Case of
XYZ .__ aenter__ ()When
XYZ .__ aexit__ ()Returns
awaitWill be done.
If you can understand the synchronously rewritten code without any problem, I think you can understand it with this explanation.
- python - parallel processing with asyncio
- python about iterative processing with specified numbers
- python - please tell me about the processing contents of opencv background subtraction
- about processing to exclude the character string group specified from list in python
- python - websocket processing using asyncio
- python - about multiple processing and loop processing in discordpy
- python 3x - (do not wait for the result) asynchronous processing × is multithread possible?
- about python argument and data definition
- python - about x-axis adjustment in matplotlib
- python - about "" "of" "" select === = "" "
- python, about the fire spread step of forest fire simulation
- python - about hamiltonian neural networks
- python - about write loop to csv
- i have a question about basic python problems
- python 3x - about downloading anaconda
- python - tkinter parallel processing
- python - about downloading youtube videos by youtube-dl
- python - what i don't understand about yolo9000 (v2)
- processing python dat files
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable