本文介绍了使用Werkzeug/Flask时如何衡量Web请求的内存使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以测量Flask/Werkzeug应用程序中任意Web请求分配的内存量?随意地说,我的意思是说我更喜欢一种技术,它可以让我以足够高的水平来检测代码,而无需更改它即可测试不同路由的内存使用情况.如果这不可能,但是仍然可以通过用一些代码包装单个请求来做到这一点.

Is there a way to measure the amount of memory allocated by an arbitrary web request in a Flask/Werkzeug app? By arbitrary, I mean I'd prefer a technique that lets me instrument code at a high enough level that I don't have to change it to test memory usage of different routes. If that's not possible but it's still possible to do this by wrapping individual requests with a little code, so be it.

在我不久前写的一个PHP应用程序中,我通过在请求的开始和结束时都调用memory_get_peak_usage()函数并加以区别来实现这一点.

In a PHP app I wrote a while ago, I accomplished this by calling the memory_get_peak_usage() function both at the start and the end of the request and taking the difference.

Python/Flask/Werkzeug中是否有类似物?如果重要的话,请使用Python 2.7.9.

Is there an analog in Python/Flask/Werkzeug? Using Python 2.7.9 if it matters.

推荐答案

首先,应该了解PHP和Python请求处理之间的主要区别.粗略地说,每个PHP工作者仅接受一个请求,处理该请求然后死亡(或重新初始化解释器). PHP是直接为此设计的,其本质是请求处理语言.因此,测量每个请求的内存使用情况非常简单.请求的峰值内存使用率等于工作程序峰值内存使用率.这是一种语言功能.

First of all, one should understand the main difference between PHP and Python requests processing. Roughly speaking, each PHP worker accepts only one request, handle it and then die (or reinit interpreter). PHP was designed directly for it, it's request processing language by its nature. So, it's pretty simple to measure per request memory usage. Request's peak memory usage is equal to the worker peak memory usage. It's a language feature.

同时,Python通常使用另一种方法来处理请求.有两个主要模型-同步和异步请求处理.但是,在测量每个请求的内存使用量时,它们都有相同的困难.原因是一名Python工作者在其一生中处理了大量请求(同时或顺序).因此,很难准确获取请求的内存使用情况.

At the same time, Python usually uses another approach to handle requests. There are two main models - synchronous and asynchronous request processing. However, both of them have the same difficulty when it comes to measure per request memory usage. The reason is that one Python worker handles plenty of requests (concurrently or sequentially) during his life. So, it's hard to get memory usage exactly for a request.

但是,可以调整底层框架和应用程序代码来完成收集内存使用情况的任务.一种可能的解决方案是使用某种事件.例如,可以在以下位置引发抽象的mem_usage事件:在请求之前,在视图函数的开始处,在视图函数的末尾,在业务逻辑中的某些重要位置,等等.然后,它应该存在此类事件的订阅者,然后执行下一步:

However, one can adapt an underlying framework and application code to accomplish collecting memory usage task. One possible solution is to use some kind of events. For example, one can raise an abstract mem_usage event on: before request, at the beginning of a view function, at the end of a view function, in some important places within the business logic and so on. Then it should exists a subscriber for such events, doing the next thing:

import resource
mem_usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

该订户必须累积此类使用数据,并在app_request_teardown/after_request上将其包含有关当前request.endpoint或路由或其他信息的信息发送到度量收集系统.

This subscriber have to accumulate such usage data and on the app_request_teardown/after_request send it to the metrics collection system with information about current request.endpoint or route or whatever.

此外,使用内存分析器是一个好主意,但通常不是用于生产用途.

Also, using a memory profiler is a good idea, but usually not for a production usage.

进一步了解请求处理模型:

Further reading about request processing models:

  • CGI
  • FastCGI
  • PHP specific

这篇关于使用Werkzeug/Flask时如何衡量Web请求的内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:28