简体   繁体   English

这两个 python 异步代码示例有什么区别? 我试图理解为什么使用 aiohttp 而不是请求

[英]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.我不清楚为什么使用 aiohttp 而不是请求。 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?这段使用请求的代码也有效,有人可以向我解释一下区别以及为什么第二个示例不是真正正确使用 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.我不清楚为什么使用 aiohttp 而不是请求。 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?这段使用请求的代码也有效,有人可以向我解释一下区别以及为什么第二个示例不是真正正确使用 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.我不清楚为什么使用 aiohttp 而不是请求。 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?这段使用请求的代码也有效,有人可以向我解释一下区别以及为什么第二个示例不是真正正确使用 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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 这两个(Python)代码示例之间有什么区别? - What is the difference between these two (Python) code samples? 这些python代码样本之间的区别? - Difference between these python code samples? Python aiohttp (with asyncio) 发送请求非常缓慢 - Python aiohttp (with asyncio) sends requests very slowly Python aiohttp 返回与 python 请求不同的响应。 我需要帮助了解原因 - Python aiohttp returns a different reponse than python requests. I need help understanding why 我不明白这两行代码之间的区别以及它们为什么返回不同的结果 - I don't understand the difference between these two lines of code and why they return different results 我是 python 的新手,想了解 class 和 object 之间的区别 - I am new to python & trying to understand the difference between class and object 我不明白这两行代码之间的区别 - I don't understand the difference between these two lines of code Python:我试图找到列表中两个元素之间的最大差异 - Python: I'm trying to find the maximum difference between two elements in a list 这两段Python代码有什么区别? - What is the difference between these two pieces of Python code? Python(初学者),我不明白这两者之间的区别 - Python(beginner), I dont understand difference between two of these
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM