简体   繁体   中英

what is the difference between these two python asyncio code samples? i'm trying to understand why aiohttp is used rather than requests

I have seen an example of code that looks like this:

import json
import os
import aiohttp
import asyncio

tokens = ['token1', 'token2', 'token3']

async def main():
    async with aiohttp.ClientSession() as session:
        await asyncio.gather(*[exchange_token(session, i) for i in tokens])

async def exchange_token(session, refresh_token):
    params={'grant_type':'refresh_token',
    'refresh_token':refresh_token,
    'client_secret':os.environ['CLIENT_SECRET'],
    'client_id':os.environ['CLIENT_ID']}

    async with session.post('https://test.com/token', data=params) as response:
        jdata = await response.json()
        print(jdata)

asyncio.run(main())

It's not clear to me exactly why aiohttp is used instead of requests. This code using requests also works, can someone please explain to me the difference and why this second example is not really correct use of asyncio?

import json
import os
import requests
import asyncio

tokens = ['token1', 'token2', 'token3']

async def main():
    await asyncio.gather(*[exchange_token(i) for i in tokens])

async def exchange_token(refresh_token):
    params={'grant_type':'refresh_token',
    'refresh_token':refresh_token,
    'client_secret':os.environ['CLIENT_SECRET'],
    'client_id':os.environ['CLIENT_ID']}

    response = await api_request(params)
    jdata = json.loads(response.text)
    print(jdata)

async def api_request(params):
    response = requests.post('https://test.com/token', data=params)
    return response

asyncio.run(main())

I have seen an example of code that looks like this:

import json
import os
import aiohttp
import asyncio

tokens = ['token1', 'token2', 'token3']

async def main():
    async with aiohttp.ClientSession() as session:
        await asyncio.gather(*[exchange_token(session, i) for i in tokens])

async def exchange_token(session, refresh_token):
    params={'grant_type':'refresh_token',
    'refresh_token':refresh_token,
    'client_secret':os.environ['CLIENT_SECRET'],
    'client_id':os.environ['CLIENT_ID']}

    async with session.post('https://test.com/token', data=params) as response:
        jdata = await response.json()
        print(jdata)

asyncio.run(main())

It's not clear to me exactly why aiohttp is used instead of requests. This code using requests also works, can someone please explain to me the difference and why this second example is not really correct use of asyncio?

import json
import os
import requests
import asyncio

tokens = ['token1', 'token2', 'token3']

async def main():
    await asyncio.gather(*[exchange_token(i) for i in tokens])

async def exchange_token(refresh_token):
    params={'grant_type':'refresh_token',
    'refresh_token':refresh_token,
    'client_secret':os.environ['CLIENT_SECRET'],
    'client_id':os.environ['CLIENT_ID']}

    response = await api_request(params)
    jdata = json.loads(response.text)
    print(jdata)

async def api_request(params):
    response = requests.post('https://test.com/token', data=params)
    return response

asyncio.run(main())

I have seen an example of code that looks like this:

import json
import os
import aiohttp
import asyncio

tokens = ['token1', 'token2', 'token3']

async def main():
    async with aiohttp.ClientSession() as session:
        await asyncio.gather(*[exchange_token(session, i) for i in tokens])

async def exchange_token(session, refresh_token):
    params={'grant_type':'refresh_token',
    'refresh_token':refresh_token,
    'client_secret':os.environ['CLIENT_SECRET'],
    'client_id':os.environ['CLIENT_ID']}

    async with session.post('https://test.com/token', data=params) as response:
        jdata = await response.json()
        print(jdata)

asyncio.run(main())

It's not clear to me exactly why aiohttp is used instead of requests. This code using requests also works, can someone please explain to me the difference and why this second example is not really correct use of asyncio?

import json
import os
import requests
import asyncio

tokens = ['token1', 'token2', 'token3']

async def main():
    await asyncio.gather(*[exchange_token(i) for i in tokens])

async def exchange_token(refresh_token):
    params={'grant_type':'refresh_token',
    'refresh_token':refresh_token,
    'client_secret':os.environ['CLIENT_SECRET'],
    'client_id':os.environ['CLIENT_ID']}

    response = await api_request(params)
    jdata = json.loads(response.text)
    print(jdata)

async def api_request(params):
    response = requests.post('https://test.com/token', data=params)
    return response

asyncio.run(main())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM