当我启用uwsgi线程支持并启动调度程序时,api停止工作

2024-06-01 00:50:16 发布

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

我对python,flask,nginx之类的东西还不熟悉。在

我有一个烧瓶应用程序,作为前端的API。另外,当flask应用程序启动时,我想用APScheduler启动一个计划任务。在

问题是,当我启用uwsgi线程支持并启动调度程序时,api停止工作(504网关超时)。但是调度程序的工作方式与日志文件中的一样。当我删除调度器/线程支持时,api可以正常工作,但显然我不再有调度器了。 不知怎的,我怀疑调度程序阻止了flask应用程序的正常运行?在

由于我是这些技术的新手,我会在下面发布我的设置。如果你需要更多的文件信息,请告诉我(整个过程是在一个树莓pi上运行的,api是通过局域网从我的pc上访问的)

应用程序服务

[Unit]
Description=uWSGI instance to serve app
After=network.target

[Service]
User=pi
Group=www-data
WorkingDirectory=/home/pi/flask
Environment="PATH=/home/pi/flask/appenv/bin"
ExecStart=/home/pi/flask/appenv/bin/uwsgi --ini app.ini --enable-threads

[Install]
WantedBy=multi-user.target

应用程序ini

^{pr2}$

应用程序副本

#!/usr/bin/env python3

from flask import Flask, request
from apscheduler.schedulers.background import BackgroundScheduler

import logging
logging.basicConfig(filename='logfile.log',level=logging.DEBUG)

from api.Controller import Controller
from Handler.Handler import Handler
from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor

api_controller = Controller()
handler = Handler()

def startHandlerJob():
    handler.ExecuteAllSensors()

app = Flask( __name__ )


@app.route('/app')
def apiDefinition():
    return 'API Definition: GetHumidityValues, TODO'

@app.route( "/app/GetHumidityValues", methods=["GET"] )
def GetHumidityValues():
    logging.info("app.py: API-call GetHumidityValues")
    return api_controller.GetHumidityValues()


if (__name__ == "__main__"):
    app.run(host='0.0.0.0')

executors = {
    'default': ThreadPoolExecutor(20),
    'processpool': ProcessPoolExecutor(5)
}
job_defaults = {
    'coalesce': False,
    'max_instances': 1
}

scheduler = BackgroundScheduler(daemon=True, executors=executors, job_defaults=job_defaults)
scheduler.start()
scheduler.add_job(startHandlerJob,'cron', minute='*')

日志文件.log

在警告:apscheduler.scheduler:已跳过作业“startHandlerJob(trigger:cron[minute='',下次运行时间:2019-12-18 19:01:00 CET)”的执行:已跳过最大运行实例数(1) 调试:apscheduler.scheduler:下次唤醒时间为2019-12-18 19:02:00+01:00(59.980780秒) 调试:apscheduler.scheduler:正在查找要运行的作业 警告:apscheduler.scheduler:已跳过作业“startHandlerJob(触发器:cron[minute='',下次运行时间:2019-12-18 19:02:00 CET)”已跳过:已达到最大运行实例数(1) 调试:apscheduler.scheduler:下次唤醒时间为2019-12-18 19:03:00+01:00(59.979407秒)

系统控制状态应用程序

* app.service - uWSGI instance to serve app
   Loaded: loaded (/etc/systemd/system/app.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-12-18 18:40:57 CET; 23min ago
 Main PID: 21129 (uwsgi)
    Tasks: 8 (limit: 2200)
   Memory: 22.9M
   CGroup: /system.slice/app.service
           |-21129 /home/pi/flask/appenv/bin/uwsgi --ini app.ini --enable-threads
           |-21148 /home/pi/flask/appenv/bin/uwsgi --ini app.ini --enable-threads
           |-21149 /home/pi/flask/appenv/bin/uwsgi --ini app.ini --enable-threads
           |-21150 /home/pi/flask/appenv/bin/uwsgi --ini app.ini --enable-threads
           |-21151 /home/pi/flask/appenv/bin/uwsgi --ini app.ini --enable-threads
           `-21152 /home/pi/flask/appenv/bin/uwsgi --ini app.ini --enable-threads

Dec 18 18:40:57 raspberrypi uwsgi[21129]: mapped 386400 bytes (377 KB) for 5 cores
Dec 18 18:40:57 raspberrypi uwsgi[21129]: *** Operational MODE: preforking ***
Dec 18 18:40:59 raspberrypi uwsgi[21129]: WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0xa6f900 pid: 21129 (default app)
Dec 18 18:40:59 raspberrypi uwsgi[21129]: *** uWSGI is running in multiple interpreter mode ***
Dec 18 18:40:59 raspberrypi uwsgi[21129]: spawned uWSGI master process (pid: 21129)
Dec 18 18:40:59 raspberrypi uwsgi[21129]: spawned uWSGI worker 1 (pid: 21148, cores: 1)
Dec 18 18:40:59 raspberrypi uwsgi[21129]: spawned uWSGI worker 2 (pid: 21149, cores: 1)
Dec 18 18:40:59 raspberrypi uwsgi[21129]: spawned uWSGI worker 3 (pid: 21150, cores: 1)
Dec 18 18:40:59 raspberrypi uwsgi[21129]: spawned uWSGI worker 4 (pid: 21151, cores: 1)
Dec 18 18:40:59 raspberrypi uwsgi[21129]: spawned uWSGI worker 5 (pid: 21152, cores: 1)

日志文件显示调度程序处于活动状态。但是当我尝试使用http:/raspberryipaddress/app时,答案是504网关超时响应。当我删除调度程序并禁用线程支持时,这个调用可以按需要工作。在

感谢任何帮助。可能我错过了一些显而易见的东西,因为我对所有这些东西都很陌生。 谢谢!在


Tags: app应用程序flaskhomebinenablepi调度
2条回答

好吧谢谢你的回答。我没能让它和你建议的解决方案一起工作。因此,我决定将调度程序作为单独的systemd服务运行。这样,调度不会停止flask api的工作。在

我有点犹豫是否把这个作为答案,但这里是。。。在

在uwsgi应用程序ini在设置了进程(在您的情况下是5,我使用4)之后,我还设置了 threads = 2。我不知道这是否与 enable-threads选项有任何直接关系,因为这似乎是让你的应用程序启动自己的线程,但它可能有助于uwsgi在每个进程有自己的线程。同时,UWSGI文档也指出(某处)更多的过程并不一定更好。在

同样,日志文件会显示来自调度程序的警告,即已达到正在运行的实例的最大数量。我认为这意味着你给调度程序的作业在下一次计划运行(1分钟后)之前没有完成?如果是这样的话,是不是卡在什么地方,挡住了其他东西?在

最后,如果所有其他操作都失败了,那么文档中的其他内容(UWSGI Security and availability

A common problem with webapp deployment is “stuck requests”. All of your threads/workers are stuck (blocked on request) and your app cannot accept more requests. To avoid that problem you can set a harakiri timer.It is a monitor (managed by the master process) that will destroy processes stuck for more than the specified number of seconds (choose harakiri value carefully).

最后,最后:)uwsgi有一个类似顶部的监控工具,可以方便地看到发生了什么,只是

pip install uwsgitop
uwsgitop 127.0.0.1:9191

再加上远程上网地址:显然是港口显示更多信息(我自己还没试过)。在

相关问题 更多 >