[英]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 ![]() |
0 ![]() |
SPOOF![]() |
OUT![]() |
...35:00.112283 ![]() |
0.001998 ![]() |
FLASK ![]() |
IN![]() |
...35:02.160254 ![]() |
2.047971 ![]() |
FLASK ![]() |
OUT![]() |
...35:02.167264 ![]() |
0.007010 ![]() |
DATA![]() |
IN![]() |
...35:02.168251 ![]() |
.. ![]() |
DATA DISK![]() |
IN![]() |
...35:02.168251 ![]() |
.. ![]() |
DATA DISK![]() |
OUT![]() |
...35:02.176605 ![]() |
.. ![]() |
DATA![]() |
OUT![]() |
...35:02.179534 ![]() |
.. ![]() |
MODEL ![]() |
IN![]() |
...35:02.180788 ![]() |
.. ![]() |
MODEL ![]() |
OUT![]() |
...35:02.495012 ![]() |
0.314224 ![]() |
PUBLISH![]() |
IN![]() |
...35:02.496034 ![]() |
.. ![]() |
PUBLISH![]() |
OUT![]() |
...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 ![]() |
version![]() |
---|---|
Python ![]() |
3.9.5 ![]() |
Flask ![]() |
2.0.1 ![]() |
click![]() |
8.0.0rc1 ![]() |
colorama![]() |
0.4.4 ![]() |
itsdangerous![]() |
2.0.1 ![]() |
Jinja2 ![]() |
3.0.1 ![]() |
MarkupSafe![]() |
2.0.1 ![]() |
Werkzeug![]() |
2.0.1 ![]() |
waitress![]() |
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 ![]() |
0 ![]() |
SPOOF![]() |
OUT![]() |
..00:24.823876 ![]() |
0.001999 ![]() |
FLASK ![]() |
IN![]() |
..00:26.873730 ![]() |
2.049854 ![]() |
FLASK ![]() |
OUT![]() |
..00:26.880726 ![]() |
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.