简体   繁体   English

Flask POST请求优化

[英]Flask POST request optimization

I have a flask app running on my machine, and I'm sending a post request to it from a thread on my own machine.我有一个 flask 应用程序在我的机器上运行,我正在从我自己机器上的线程向它发送一个发布请求。 The flask app then sends it to disk, trains a model on it, and pushes the data out (publish).然后 flask 应用程序将其发送到磁盘,在其上训练 model,并将数据推出(发布)。 I'm timing each step because it seems slow.我正在为每一步计时,因为它看起来很慢。 Turns out the slowest part is getting the data from my csv (called spoof).原来最慢的部分是从我的 csv 获取数据(称为欺骗)。

Module模块 end结尾 UTC now现在世界协调时间 timecost时间成本
SPOOF欺骗 IN ...35:00.110285 ...35:00.110285 0 0
SPOOF欺骗 OUT出去 ...35:00.112283 ...35:00.112283 0.001998 0.001998
FLASK FLASK IN ...35:02.160254 ...35:02.160254 2.047971 2.047971
FLASK FLASK OUT出去 ...35:02.167264 ...35:02.167264 0.007010 0.007010
DATA数据 IN ...35:02.168251 ...35:02.168251 .. ..
DATA DISK数据盘 IN ...35:02.168251 ...35:02.168251 .. ..
DATA DISK数据盘 OUT出去 ...35:02.176605 ...35:02.176605 .. ..
DATA数据 OUT出去 ...35:02.179534 ...35:02.179534 .. ..
MODEL MODEL IN ...35:02.180788 ...35:02.180788 .. ..
MODEL MODEL OUT出去 ...35:02.495012 ...35:02.495012 0.314224 0.314224
PUBLISH发布 IN ...35:02.496034 ...35:02.496034 .. ..
PUBLISH发布 OUT出去 ...35:02.496534 ...35:02.496534 .. ..

getting it to flask takes 2 seconds while training the model on new data takes less than a third of a second.将其变为 flask 需要 2 秒,而使用新数据训练 model 只需不到三分之一秒。 I must be doing something very wrong.我一定是做错了什么。

Here's the thread that sends the post request:这是发送发布请求的线程:

def run(self):
    while True:
        print('SPOOF IN', dt.datetime.utcnow())
        x = self.provideIncrementalWithId()
        print('SPOOF OUT', dt.datetime.utcnow())
        response = requests.post(
            url=f'http://localhost:{self.port}/subscription/update', 
            json=x)

here's basically the entire flask app but I think only update is relevant:这里基本上是整个 flask 应用程序,但我认为只有update是相关的:

import threading
import secrets
import satori
import datetime as dt
from flask import Flask
from flask import request
from waitress import serve
from satori.lib.engine.structs import Observation

app = Flask(__name__)
app.config['SECRET_KEY'] = secrets.token_urlsafe(16)
Engine = satori.getEngine()
Engine.run()

def spoofStreamer():
    thread = threading.Thread(target=satori.spoof.Streamr().run, daemon=True)
    thread.start()

@app.route('/subscription/update', methods=['POST'])
def update():
    print('FLASK IN', dt.datetime.utcnow())
    x = Observation(request.json)
    print('FLASK OUT', dt.datetime.utcnow())
    Engine.data.newData.on_next(x)
    return request.json

if __name__ == '__main__':
    spoofStreamer()
    serve(app, host='0.0.0.0', port=satori.config.get()['port'])

Is there something stupid I'm doing here?我在这里做了什么愚蠢的事吗? Should I not use waitress?我不应该使用女服务员吗? Why is it so slow, especially given the request is coming from the same machine?为什么这么慢,特别是考虑到请求来自同一台机器?

EDIT: I just realized maybe it's a versioning thing here are my relevant versions:编辑:我刚刚意识到这可能是版本控制问题,这是我的相关版本:

package package version版本
Python Python 3.9.5 3.9.5
Flask Flask 2.0.1 2.0.1
click点击 8.0.0rc1 8.0.0rc1
colorama色拉玛 0.4.4 0.4.4
itsdangerous这很危险 2.0.1 2.0.1
Jinja2 Jinja2 3.0.1 3.0.1
MarkupSafe标记安全 2.0.1 2.0.1
Werkzeug工匠 2.0.1 2.0.1
waitress服务员 2.0.0 2.0.0

Edit2: after updating my packages to the latest I still get 2 seconds to get the request Edit2:将我的包裹更新到最新版本后,我仍然有 2 秒的时间来获取请求

Module模块 end结尾 UTC now现在世界协调时间 timecost时间成本
SPOOF欺骗 IN ..00:24.821877 ..00:24.821877 0 0
SPOOF欺骗 OUT出去 ..00:24.823876 ..00:24.823876 0.001999 0.001999
FLASK FLASK IN ..00:26.873730 ..00:26.873730 2.049854 2.049854
FLASK FLASK OUT出去 ..00:26.880726 ..00:26.880726 0.006996 0.006996

You know what it was... I changed localhost to 127.0.0.1 in the post call and its almost instant now.你知道那是什么......我在邮寄电话中将 localhost 更改为 127.0.0.1,现在几乎是即时的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM