我在使用生产Web服务器(IIS)按计划的时间间隔启动后台任务时遇到问题。
我的应用程序可以与内置Werkzeug
网络服务器正常工作,但是当我部署到IIS时,后台任务不会每5分钟触发一次。相反,它将在加载网页后5分钟运行。这是因为我在__init__.py
中初始化了调度程序吗?
import os
from flask import Flask
from flask_apscheduler import APScheduler
from apscheduler.schedulers.background import BackgroundScheduler
app = Flask(__name__)
app.config.from_object('app.config.BaseConfig')
# Setup the database
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
scheduler = APScheduler(BackgroundScheduler())
scheduler.init_app(app)
scheduler.start()
# Import the views
from app.views import main, error
import logging
import os
SCHEDULER_JOBS = [
{
'id': 'read',
'func': 'app.read_licenses:read',
'trigger': 'interval',
'minutes': 5
}
]
class BaseConfig(object):
"""Base configuration"""
JOBS = SCHEDULER_JOBS
SCHEDULER_JOB_DEFAULTS = {
'coalesce': False,
'max_instances': 1
}
# use sqlite by default
SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL', 'sqlite:///app.db')
SCHEDULER_API_ENABLED = True
<configuration>
<system.webServer>
<handlers>
<add name="Python FastCGI"
path="*"
verb="*"
modules="FastCgiModule"
scriptProcessor="c:\inetpub\wwwroot\myapp\venv\scripts\python.exe|c:\inetpub\wwwroot\myapp\venv\lib\site-packages\wfastcgi.py"
resourceType="Unspecified"
requireAccess="Script" />
</handlers>
</system.webServer>
<appSettings>
<!-- Required settings -->
<add key="WSGI_HANDLER" value="app.app" />
<add key="PYTHONPATH" value="C:\inetpub\wwwroot\myapp" />
<!-- Optional settings -->
<add key="WSGI_LOG" value="C:\iis-logs\myapp.log" />
</appSettings>
</configuration>