【问题标题】:Does apache or flask need special setting for a web application which use pickle?apache 或flask 是否需要对使用pickle 的Web 应用程序进行特殊设置?
【发布时间】:2019-06-29 15:08:01
【问题描述】:

在 Visual Studio 中,我已经在 python 中实现了一个 web api,带有 flask,它使用 pickle 加载 SVM,然后整个系统已经部署在 apache 上。 我的问题是,当我尝试从 Visual Studio 进行系统调试时,一切正常,但是当我在 apache 服务器上加载它时,页面只是加载而没有给出结果或错误。
Web应用程序的代码报告如下:

import PythonApplication2 as ml
import requests
import json
from flask_cors import CORS, cross_origin
from flask import Flask, jsonify, request, render_template
import pickle as pk

MLwebapp = Flask(__name__)

left_model=pk.load(open('C:\\myapp\\app\\left_classifier.pickle','rb'))
right_model=pk.load(open('C:\\myapp\\app\\right_classifier.pickle','rb'))

wsgi_app = MLwebapp.wsgi_app
# Make the WSGI interface available at the top level so wfastcgi can get it.


@MLwebapp.route('/')
def start():
    return render_template('WebPage1.html')

@MLwebapp.route('/test')
def hello():
    number='203'
    list_left=[]
    list_right=[]
    for i in range(0,19):
        data=requests.get('url here'+number+str(i))
        json_data=json.loads(data.json())
        number=json.loads(json_data['Dati'])
        left_temp=[x[1] for x in number]
        right_temp=[z[2] for z in number]
        list_left.append(left_temp)
        list_right.append(right_temp)
    patient_vector_right,patient_vector_left=ml.funzionetotale(list_left,list_right)
    patient_vector_left=patient_vector_left.reshape(1, -1)
    patient_vector_right=patient_vector_right.reshape(1, -1)
    prediction=ml.machine_learning(patient_vector_left,patient_vector_right,left_model,right_model)
    risultato="Risultato: " + prediction
    return jsonify(risultato)

if __name__ == '__main__':
    import os
    HOST = os.environ.get('SERVER_HOST', 'localhost')
    try:
        PORT = int(os.environ.get('SERVER_PORT', '8080'))
    except ValueError:
        PORT = 8080
    MLwebapp.run(HOST, PORT)

模板WebPage1.html非常简单,只需要一个按钮即可测试系统:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Ciao</title>
</head>
<body>
    <h1>Test button2</h1>
    <form method="get" action="/test">
        <input type="submit" value="Press for test"/>
    </form>
</body>
</html>

关于 apache 和 WSGI 的设置:
在 httpd.conf 中:

added  
LoadFile "c:/program files (x86)/microsoft visual studio/shared/python36_64/python36.dll"
LoadModule wsgi_module "c:/myapp/flask/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIPythonHome "c:/myapp/flask"  
Removed the # from LoadModule cgi_module modules/mod_cgi.so  
added  
Listen 8080
Removed the # from conf/extra/httpd-vhosts.conf

在 httpd-vhosts.conf 中:

added  
<VirtualHost *:8080>
        ServerAdmin localhost
        ServerName  localhost:8080
        WSGIScriptAlias / "C:/myapp/app/web.wsgi"
        DocumentRoot "C:/myapp/app"
        <Directory "C:/myapp/app">
                Require all granted
        </Directory>
        ErrorLog "C:/myapp/app/logs/error.log"
        CustomLog "C:/myapp/app/logs/access.log" common
</VirtualHost>

在应用根目录中创建 web.wsgi 文件:

import sys

sys.path.insert(0, 'C:/myapp/app')

from app import MLwebapp as application

通过一些测试,我了解到问题在于 wsgi 和 pickle 之间的关系,但我找不到可以帮助我解决问题的指南或帖子。

【问题讨论】:

  • 您在日志中看到了什么?
  • 错误日志中没有任何内容和访问日志中的“::1 - - [29/Jun/2019:18:01:03 +0200]”-“408 -”

标签: python apache flask pickle wsgi


【解决方案1】:

一种方法是排除泡菜的问题。一种简单的方法是捕获尝试加载泡菜时发生的任何异常,并将这些异常传递给您的起始模板。类似的东西

try:
    left_model=pk.load(open('C:\\myapp\\app\\left_classifier.pickle','rb'))
    left_model_ex = None
except Exception as ex:
    left_model = None
    left_model_ex = repr(ex)

right_model 也一样)

left_model_ex 传递到模板中,执行类似的操作

{% if left_model_ex %}<div>{{ left_model_ex }}</div>{% endif %}

可见的地方。

【讨论】:

  • 谢谢您的回答,我尝试了您建议的方法,我注意到系统随机运行一次,没有发现异常。电脑重启后它工作正常(应该从原点开始工作),我试图再次重新启动电脑,但第二次没有工作,似乎真的很随机​​地工作,这件事让我大吃一惊。 p.s.当系统卡住我需要重新启动apache服务器,否则它甚至不会加载网页模板。
  • 在这种情况下,我怀疑内存不足。进程监视器显示什么?
  • 进程监视器没有特别显示任何内容,chrome 的内存使用量低于 5%,apache http 服务器的内存使用量为 1.2%,但我注意到当我从以下位置调用 http 时系统停止工作谷歌浏览器的新标签,如果我只是停留在同一个标​​签上,系统在每次通话时都能正常工作,这可能是我混淆随机性的问题吗?
  • EDIT 删除了我上一条评论的最后一部分,新的一系列测试表明不工作仍然是随机的。
  • 您是否有可能遇到以下情况:/test 的两个请求被并行处理,而 ml.funzionetotale 改变了全局变量?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多