You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using asyncio is more than just putting async in front of the function doing the http request. In fact this is the completely wrong way to use asyncio and will actually destroys performance as this blocks the event loop from doing any other work while the http request is being made.
To use asyncio correctly the tcp socket sending the data must be using asyncio and one of the low level asyncio socket functions. https://docs.python.org/3/library/asyncio-stream.html These are awaited, allowing the event loop to do other work.
What you have suggested is EXTREMELY bad practice and will stall the servers of anyone who uses it. This is why you can't use requests or urllib with asyncio. You have to use something like aiohttp.
The right way to use asyncio with your library is to run it in a thread pool executor so that the blocking IO stays off the main thread.
You would be better off deleting that example than keeping it in it's current form.
Reacted by Dmytro Smyk, Fedir Alifirenko, Guillaume Gelin, cake-monotone, David Acevedo, Urbanonymous, Brandon Fisher, RoyLeviLangware, Edwin Heerschap, Fredrik Ryden and 5 more
@agronick is right. The async example is wrong and misleading for less experienced developers. And I agree with @urbanonymous that it's best using the API directly.
I prepared an example of SendGrid client that sends async emails using httpx:
Activity
[-]Asyncio example is completely wrong[/-][+]Asyncio example is completely wrong, update[/+]thinkingserious commentedon Apr 27, 2021
Hello @agronick,
Thank you for raising this issue!
This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.
Here are some related issues and PRs to consider upon update:
With best regards,
Elmer
agronick commentedon Apr 28, 2021
#953 is exactly what I'm talking about but it was closed.
parikls commentedon Sep 24, 2021
upvote. unfortunately current SDK is not compatible with asyncio which makes it unusable with such things like aiohttp or fastapi
dacevedo12 commentedon May 30, 2024
Hi, it's 2024 and the example is still wrong. Open to PRs? I can help
It's as simple as wrapping the call using asyncio.to_thread so it doesn't block the main thread.
Ideally though the SDK itself would provide support for asyncio, maybe through a transport layer that uses aiohttp or httpx
urbanonymous commentedon Jun 1, 2024
I'll just use the api lol
Fix sendgrid#988: Remove async example
RobertoPrevato commentedon Apr 10, 2025
@agronick is right. The async example is wrong and misleading for less experienced developers. And I agree with @urbanonymous that it's best using the API directly.
I prepared an example of SendGrid client that sends
asyncemails usinghttpx:PowerShell:
Bash:
SENDGRID_API_KEY="<YOUR_API_KEY>" python example.pyTip
Do not instantiate a new
AsyncClientevery time you want to use it, reuse one in your application lifetime.If in doubt, read about TCP connection pooling. https://www.python-httpx.org/advanced/clients/#why-use-a-client
Tip
To understand why the example in this repository is wrong, watch this: Ryan Dahl: Original Node.js presentation.