[英]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.