使用带有烧瓶的 APScheduler 运行计划任务(使用 mod_wsgi)



我正在尝试每天在烧瓶中的特定时间在后台发送电子邮件。当我添加作业时,该应用程序几乎挂起,我认为我在线程方面遇到了问题。配置如下所示

jobstores = {
'default': SQLAlchemyJobStore(url='path_to_my_db')
}
executors = {
'default': ThreadPoolExecutor(5),
'processpool': ProcessPoolExecutor(3)
}
job_defaults = {
'coalesce': False,
'max_instances': 3
}
scheduler = BackgroundScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone=utc)
scheduler.start()

然后我添加我的工作

def send_reports():
msg = Message("Microphone testing 1, 2",
recipients=["me@mycompany.com"])
mail.send(msg)
scheduler.add_job(send_reports, 'cron', hour=8, minute=23)

如果我注释掉scheduler.add_job行,应用程序可以正常运行

在虚拟主机中,我有线条

WSGIDaemonProcess www.mycomapny.com processes=2 threads=5
WSGIScriptAlias / /var/www/html/myapp.wsgi

将感谢您的帮助

我终于设法用APSchedular发送电子邮件。

我在 Apache 虚拟主机中允许多个线程的设置(正在使用 mod_wsgi(

WSGIDaemonProcess app threads=15 maximum-requests=10000
WSGIScriptAlias / /var/www/html/myapp.wsgi
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}

然后在我的应用程序中,我首先导入后台调度程序

from apscheduler.schedulers.background import BackgroundScheduler

使用时区实例化我的调度程序,但使用所有其他默认配置

scheduler = BackgroundScheduler(timezone='Africa/Nairobi')

然后在第一次请求之前,我启动调度程序并添加send_reports作业

@app.before_first_request
def initialize():
scheduler.start()
scheduler.add_job(send_reports, 'cron', hour=10, minute=10, end_date='2055-05-30')

使用 pdfkit 和 flask-email 将报告作为 pdf 附件发送是另一回事,但它的要点是安装正确版本的 wkhtmltopdf 并拥有正确的 env 路径,并确保您将应用程序上下文传递给 flask-mail 以在后台线程中发送邮件。

因此,这会在每天上午 1010 点将报告发送到指定的电子邮件。希望有人觉得这有帮助

最新更新