Skip to content

Asyncio example is completely wrong, update #988

Open
@agronick

Description

@agronick

Issue Summary

I'm talking about this example https://github.com/sendgrid/sendgrid-python/blob/main/use_cases/asynchronous_mail_send.md

Steps to Reproduce

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions