0
I would like to call a class that does a post for an Endpoint but that this call was not blocking, because it is inside an event that sometimes triggers 5 cohorts in a row and I can’t wait for the Post’s conclusion to continue.
I did it this way, but I’m having a hard time testing if it’s really working
Main class.py
"""
códigos que não interessa no exemplo
"""
from def_api import Api
api = Api('teste')
for item in range(10): #for ficticio
api.Send('valores')
def_api.py class
import requests
class Api(object)
def __init__(self, valor1):
self.__valor1 = valor1
async def Send(self, valor2):
self.__valor2 = valor2
data = {'valor1': self.__valor1, 'valor2': self.__valor2}
r = request.post(url, json=data)
return await r.status_code
I was in doubt: whether the await should be placed on the request, r = await request.post.. or if I should take the await so it doesn’t get blocking..
I ask for help, links, etc.
The request.post() call will block the main thread even if the function is async. You would have to call "await request.post()" to not block And request.post() would have to be itself an async function, which I don’t think it is. It would have to use threads or, better yet, asyncio to achieve this result. This OS question in English has some examples: https://stackoverflow.com/questions/22190403/how-could-i-use-requests-in-asyncio
– epx