How to implement for using Asyncio?

Good day. I can not understand how to implement the loop using Asyncio.
Let's say there are 5 queries for which you first need to do the first request and get the authorization code, then using it to make a second request. I want it to be implemented using Asyncio, not to wait for answers. How to do it, tell me.
March 23rd 20 at 19:01
1 answer
March 23rd 20 at 19:03
Solution
To give the most correct answer, it is desirable to know the details of the task. But in the most General case this is solved by a chain of coroutines:
third_task def async():
 # Some asynchronous actions
 return something

async def second_task():
 r = await third_task()
 # Some actions with the result in variable r
 return processed_r

async def first_task():
 r = await second_task()
 # Some actions with the result in variable r

asyncio.run(first_task())
I apologize that at once did not write. But already there he will understand, thank you.
Roughly speaking, a 2 post and get, where we use the answer from the first post.
def f1():
r = requests.post(site.ru)
return r.text
def f2(param):
r = requests.post(site.ru, params=param)
return r.text

The only thing it is better to use aiohttp or you can do requests? - krystal_Ward commented on March 23rd 20 at 19:06
@krystal_Ward, will not work, the library requests synchronous and just put a stake event loop. Will have to use the asynchronous library, like aiohttp. - Dahlia commented on March 23rd 20 at 19:09
@Dahlia, thank you very much. Best off the dock to learn or have a normal material? - krystal_Ward commented on March 23rd 20 at 19:12
@krystal_Ward, the official documentation is good - simple and easy to understand. - Dahlia commented on March 23rd 20 at 19:15
@Dahlia, thank you - krystal_Ward commented on March 23rd 20 at 19:18
@Dahlia, I'm sorry for another stupid question. Does not know how to organize aiohttp on the subject. As described above, do I need to do in parallel the same scenario that assumed of the first request and the second, which uses the response from the first. - krystal_Ward commented on March 23rd 20 at 19:21
@Dahlia,
async def get_token():
 async with aiohttp.ClientSession() as session:
 async with the session.post(site, data=payload, headers=headers, ssl=sslcontext) as resp:
 text = await resp.text()
 return text

async def get_data(number):
 access_token = get_token await()
 async with aiohttp.ClientSession() as session:
 async with the session.get(site2, params=payload1, headers=headers1, ssl=sslcontext) as resp:
 text = await resp.text()
 print('{:.70}...{}'.format(text, number))
 return number, text


I wanted to ask whether I'm doing that 2 times to use async with aiohttp.ClientSession() as session in each function? - krystal_Ward commented on March 23rd 20 at 19:24
@krystal_Ward, it is permissible, but the best session to transfer from the starting to the called coroutines:
async def get_token(session):
 async with the session.post(site, data=payload, headers=headers, ssl=sslcontext) as resp:
....

async def get_data(number):
 async with aiohttp.ClientSession() as session:
 access_token = get_token await(session)
 ...
- Dahlia commented on March 23rd 20 at 19:27
@Dahlia, just recently found this option. thank you) - krystal_Ward commented on March 23rd 20 at 19:30

Find more questions by tags Asynchronous programmingPython