调度程序在IIS上工作不正常

2024-04-26 22:59:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用生产Web服务器(IIS)按计划时间间隔启动后台任务时遇到问题

我的应用程序可以与内置的Werkzeugweb服务器正常工作,但当我部署到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

config.py

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

web.config

<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>

Tags: frompyimportaddconfigappflaskread