我尝试让它工作,但一定有更好的方法,欢迎任何建议。
我正在尝试在我的Python金字塔应用程序中使用pyramid_mailer(存储在.ini文件中的设置)和apscheduler设置计划发送定时邮件。
我还使用SQLAlchemyJobStore,以便在应用程序重新启动时可以重新启动作业。
jobstores = {
'default': SQLAlchemyJobStore(url='mysql://localhost/lgmim')
}
scheduler = BackgroundScheduler(jobstores=jobstores)
@view_config(route_name='start_email_schedule')
def start_email_schedule(request):
# add the job and start the scheduler
scheduler.add_job(send_scheduled_email, 'interval', [request], weeks=1)
scheduler.start()
return HTTPOk()
def send_scheduled_email(request):
# compile message and recipients
# send mail
send_mail(request, subject, recipients, message)
def send_mail(request, subject, recipients, body):
mailer = request.registry['mailer']
message = Message(subject=subject,
recipients=recipients,
body=body)
mailer.send_immediately(message, fail_silently=False)
我已经做到这一步了,现在出现了一个错误,可能是因为无法pickle请求。
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
使用pyramid.threadlocal.get_current_registry().settings
可以在第一次获取邮件服务,但之后会报错。建议不要在任何情况下使用该方法。
还有其他的方法吗?