'Load messages queued for sending, and send them.'

def flush(self):

    fetch_messages = (lambda : self.db.all(u'\n                                                SELECT    *\n                                                        FROM    email_queue\n                                                    WHERE    not    dead\n                                        ORDER    BY    id    ASC\n                                                    LIMIT    60\n                                '))
    nsent = 0
    while True:
        messages = fetch_messages()
        if (not messages):
            break
        for rec in messages:
            try:
                r = self._flush_one(rec)
            except:
                self.db.run(u'UPDATE    email_queue    SET    dead=true    WHERE    id    =    %s', (rec.id,))
                raise
            self.db.run(u'DELETE    FROM    email_queue    WHERE    id    =    %s', (rec.id,))
            if (r == 1):
                sleep(self.sleep_for)
            nsent += r
    return nsent
