Skip to content

Instantly share code, notes, and snippets.

@phpdude
Last active April 6, 2023 05:05

Revisions

  1. Alexandr Shurigin revised this gist Jul 3, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    ## Simplest Async Email Backend for Django

    ### What is this?

    It is simplest email backend for Django which supports async email delivery in parallel threads. Keep it simple! It has various analogs, you can google for it yourself.
  2. Alexandr Shurigin revised this gist Jul 3, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ### What is this?

    It is simple, fast and clean Django's email backend which supports async sending of emails in parallel threads. Keep it simple! It has various analogs, you can google for it yourself.
    It is simplest email backend for Django which supports async email delivery in parallel threads. Keep it simple! It has various analogs, you can google for it yourself.

    ### Requirements

  3. Alexandr Shurigin created this gist Jul 3, 2016.
    16 changes: 16 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    ### What is this?

    It is simple, fast and clean Django's email backend which supports async sending of emails in parallel threads. Keep it simple! It has various analogs, you can google for it yourself.

    ### Requirements

    Django async email backend requires futures library. So you need to install it with `pip install futures`.

    ### Django configuration settings

    This is simple backend so you need to configure django to use our backend and you can alter pool size with `EMAIL_BACKEND_POOL_SIZE`.

    ``` python
    EMAIL_BACKEND = 'utils.email.EmailBackend'
    EMAIL_BACKEND_POOL_SIZE = 15 # default value
    ```
    19 changes: 19 additions & 0 deletions email.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    from concurrent import futures
    from django.conf import settings
    from django.core.mail.backends.smtp import EmailBackend as BaseEmailBackend


    class EmailBackend(BaseEmailBackend):
    """
    A wrapper that manages the SMTP network connection asynchronously
    """

    def __init__(self, *args, **kwargs):
    super(EmailBackend, self).__init__(*args, **kwargs)

    self.executor = futures.ThreadPoolExecutor(
    max_workers=int(getattr(settings, 'EMAIL_BACKEND_POOL_SIZE', 15))
    )

    def send_messages(self, email_messages):
    return self.executor.submit(super(EmailBackend, self).send_messages, email_messages)