def send_loop(self, connection, sleep_time):

        # This could be /improved by looking up the last notified person
        last_sent = None

        while True:

            started_sending_at = datetime.now()
            self.logger.info(
                "Starting send loop at %s" %
                str(started_sending_at))
            if last_sent:
                user_settings = models.Settings.objects.filter(
                    interval__lte=(
                        (started_sending_at -
                         last_sent).seconds //
                        60) //
                    60).order_by('user')
            else:
                user_settings = None

            self.send_mails(connection, last_sent=last_sent, user_settings=user_settings)

            connection.close()
            last_sent = datetime.now()
            elapsed_seconds = (last_sent - started_sending_at).seconds
            time.sleep(
                max(
                    (min(nyt_settings.INTERVALS)[0] - elapsed_seconds) * 60,
                    sleep_time,
                    0
                )
            )
dataset/ETHPy150Open benjaoming/django-nyt/django_nyt/management/commands/notifymail.py/Command.send_loop/original