def _store_result(self, name, res_id, res, callback=None): logger.debug('Got result back for %s' % res_id) self._in_progress[name].remove(res_id) success, result = res self._results[res_id] = res if not success: logger.error(result) if callback is not None: callback(name, res_id, success, result)
def apply_async(self, name, func, args=None, callback=None): if args is None: args = tuple() res_id = str(uuid4()) self._in_progress[name].append(res_id) logger.debug('Running %s async - id: %s' % (func, res_id)) async_callback = partial(self._store_result, name, res_id, callback=callback) cmd = partial(_run, func) res = self._pool.apply_async(cmd, args, callback=async_callback) time.sleep(.1) if res.ready() and not res.successful(): try: res.get() except Exception, e: async_callback((False, str(e)), callback=callback)
def send_email(tos, subject, body, smtp_config): msg = MIMEText(body.encode("utf8"), "plain", "utf-8") msg["To"] = email.utils.formataddr(("Recipient", ",".join(tos))) msg["From"] = email.utils.formataddr(("Author", smtp_config["from"])) msg["Subject"] = Header(subject, "utf8") server = smtplib.SMTP(smtp_config["host"], smtp_config.get("port", 25)) try: server.ehlo() if server.has_extn("STARTTLS"): server.starttls() server.ehlo() username = smtp_config.get("username") if username is not None: server.login(username, smtp_config["password"]) server.sendmail(smtp_config["from"], tos, msg.as_string()) logger.debug("Mail sent") except Exception: logger.exception("Could not send the email") finally: server.quit()