简体   繁体   English

为什么 pypy flask 应用程序比 cpython 慢?

[英]Why is pypy flask aplication slower than cpython?

I have a flask application (app.py) which returns a string,我有一个 flask 应用程序 (app.py),它返回一个字符串,

from flask import Flask
import os

app = Flask(__name__)

@app.route("/")
def hello():
    return "Flask inside Docker!!"


if __name__ == "__main__":
    port = int(os.environ.get("PORT", 6000))
    app.run(debug=True,host='0.0.0.0',port=port)

I ran this application using the below dockerfile content, which gave me the throughput of 320/sec我使用以下 dockerfile 内容运行此应用程序,这给了我320/秒的吞吐量

FROM opensuse/leap:15.3
RUN zypper -n install python3 python3-pip python3-Flask
COPY . /app
WORKDIR /app
ENTRYPOINT ["python3"]
CMD ["app.py"]

However with pypy3, I got only 200/sec throughput , the docker file is given below.但是使用 pypy3,我只有 200/sec 吞吐量,下面给出了 docker 文件。

FROM opensuse/leap:15.3
FROM pypy:3
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["pypy3"]
CMD ["app.py"]

where requirements.txt contains only "flask".其中 requirements.txt 仅包含“烧瓶”。

What could be the reason for this performance degradation?这种性能下降的原因可能是什么?

Two things comes to mind.我想到了两件事。

  1. It could be that running a basic app that just return a string doesn't really make use of pypy JIT compilation and other pypy optimizations.可能是运行一个只返回字符串的基本应用程序并没有真正利用 pypy JIT 编译和其他 pypy 优化。 And in this specific case pypy interpreter is running slower在这种特定情况下,pypy 解释器运行速度较慢
  2. Are you wrapping flask with gunicorn?你用 gunicorn 包装 flask 吗? if so and even if you dont, could it be that you set it up in a way that each invocation is executed in a new process?如果是这样,即使您不这样做,是否可以以每次调用都在新进程中执行的方式进行设置? If each invocation is a new process, pypy JIT warmup is lost between each invocation, which means JIT is basically not working and you are just paying high price of pypy trying to JIT.如果每次调用都是一个新进程,则 pypy JIT 预热会在每次调用之间丢失,这意味着 JIT 基本上不起作用,您只是在为尝试 JIT 的 pypy 付出高昂的代价。

Brushing aside the fact that PyPy might really be intrinsically slower for your case, there are some factors that could be making it unnecessarily slower:撇开PyPy本质上对您的情况来说可能确实较慢这一事实,有一些因素可能会使其不必要地变慢:

  • Profiling is known to slow PyPy a lot more than CPython .众所周知,与CPython相比,性能分析PyPy变慢很多。
  • Some debugging/logging code can disable optimizations (by, eg, forcing frames).一些调试/日志代码可以禁用优化(例如,通过强制帧)。
  • The server you're using can be a dominant factor in performance (think about how awful classic CGI would be with a JIT: it would never warm up).您使用的服务器可能是影响性能的主要因素(想想经典 CGI 和 JIT 会有多糟糕:它永远不会预热)。 It can also simply influence results (different WSGI servers have shown various speed-ups).它也可以简单地影响结果(不同的WSGI服务器已经显示出不同的加速)。
  • Old-style classes are slower than new-style ones.旧式课程比新式课程慢。
  • Even if everything is in memory, you could be hitting eg slow paths in PyPy's SQLite.即使所有内容都在 memory 中,您也可能遇到 PyPy 的 SQLite 中的慢速路径。

A nightly build will probably be faster too, as there are many improvements relative to 1.5.每晚构建可能也会更快,因为相对于 1.5 有很多改进。

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

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