- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
设置:Python 2.7.15、Tornado 5.1
我有一台网络服务器,可以处理 ~40 /recommend
每秒请求数。平均响应时间为 25 毫秒,但差异很大(某些请求可能需要超过 500 毫秒)。
每个请求在内部生成 1-8 个 Elasticsearch 查询(HTTP 请求)。每个 Elasticsearch 查询可能需要 1-150 毫秒。
Elasticsearch 请求通过 elasticsearch-dsl 同步处理图书馆。
目标是减少 I/O 等待时间(对 Elasticsearch 的查询)并每秒处理更多请求,这样我就可以减少机器数量。有一点是 Not Acceptable - 我不想增加平均处理时间(25 毫秒)。
我在网络上发现了一些 Tornado -elasticsearch 实现,但由于我只需要使用 Elasticsearch 的一个端点 ( /_search
),我试图单独完成这一任务。
下面是我的网络服务器的退化实现。在相同负载(每秒约 40 个请求)的情况下,平均请求响应时间增加到 200 毫秒!
深入研究后,我发现内部异步处理时间(对 Elasticsearch 的查询)并不稳定,每个 fetch
所花费的时间调用可能会有所不同,并且总平均值(在 ab
负载测试中)很高。
我正在使用ab
模拟负载并通过打印电流 fetch
在内部测量负载平均处理时间 fetch
处理时间和最大处理时间。一次执行一个请求时(并发度 1): ab -p es-query-rcom.txt -T application/json -n 1000 -c 1 -k 'http://localhost:5002/recommend'
我的打印看起来像:[avg req_time: 3, dur: 3] [current req_time: 2, dur: 3] [max req_time: 125, dur: 125] reqs: 8000
但是当我尝试增加并发数(最多 8 个)时:ab -p es-query-rcom.txt -T application/json -n 1000 -c 8 -k 'http://localhost:5002/recommend'
现在我的打印看起来像:[avg req_time: 6, dur: 13] [current req_time: 4, dur: 4] [max req_time: 73, dur: 84] reqs: 8000
平均请求现在慢了2倍(根据我的测量,速度是x4)!我在这里想念什么?为什么我会看到这种退化?
web_server.py:
import tornado
from tornado.httpclient import AsyncHTTPClient
from tornado.options import define, options
from tornado.httpserver import HTTPServer
from web_handler import WebHandler
SERVICE_NAME = 'web_server'
NUM_OF_PROCESSES = 1
class Statistics(object):
def __init__(self):
self.total_requests = 0
self.total_requests_time = 0
self.total_duration = 0
self.max_time = 0
self.max_duration = 0
class RcomService(object):
def __init__(self):
print 'initializing RcomService...'
AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient", max_clients=3)
self.stats = Statistics()
def start(self, port):
define("port", default=port, type=int)
db = self.get_db(self.stats)
routes = self.generate_routes(db)
app = tornado.web.Application(routes)
http_server = HTTPServer(app, xheaders=True)
http_server.bind(options.port)
http_server.start(NUM_OF_PROCESSES)
tornado.ioloop.IOLoop.current().start()
@staticmethod
def generate_routes(db):
return [
(r"/recommend", WebHandler, dict(db=db))
]
@staticmethod
def get_db(stats):
return {
'stats': stats
}
def main():
port = 5002
print('starting %s on port %s', SERVICE_NAME, port)
rcom_service = RcomService()
rcom_service.start(port)
if __name__ == '__main__':
main()
web_handler.py:
import time
import ujson
from tornado import gen
from tornado.gen import coroutine
from tornado.httpclient import AsyncHTTPClient
from tornado.web import RequestHandler
class WebHandler(RequestHandler):
def initialize(self, db):
self.stats = db['stats']
@coroutine
def post(self, *args, **kwargs):
result = yield self.wrapper_innear_loop([{}, {}, {}, {}, {}, {}, {}, {}]) # dummy queries (empty)
self.write({
'res': result
})
@coroutine
def wrapper_innear_loop(self, queries):
result = []
for q in queries: # queries are performed serially
res = yield self.async_fetch_gen(q)
result.append(res)
raise gen.Return(result)
@coroutine
def async_fetch_gen(self, query):
url = 'http://localhost:9200/my_index/_search'
headers = {
'Content-Type': 'application/json',
'Connection': 'keep-alive'
}
http_client = AsyncHTTPClient()
start_time = int(round(time.time() * 1000))
response = yield http_client.fetch(url, method='POST', body=ujson.dumps(query), headers=headers)
end_time = int(round(time.time() * 1000))
duration = end_time - start_time
body = ujson.loads(response.body)
request_time = int(round(response.request_time * 1000))
self.stats.total_requests += 1
self.stats.total_requests_time += request_time
self.stats.total_duration += duration
if self.stats.max_time < request_time:
self.stats.max_time = request_time
if self.stats.max_duration < duration:
self.stats.max_duration = duration
duration_avg = self.stats.total_duration / self.stats.total_requests
time_avg = self.stats.total_requests_time / self.stats.total_requests
print "[avg req_time: " + str(time_avg) + ", dur: " + str(duration_avg) + \
"] [current req_time: " + str(request_time) + ", dur: " + str(duration) + "] [max req_time: " + \
str(self.stats.max_time) + ", dur: " + str(self.stats.max_duration) + "] reqs: " + \
str(self.stats.total_requests)
raise gen.Return(body)
我尝试使用异步类( Simple
与 curl
), max_clients
大小,但我不明白在我的情况下什么是最好的曲调。但是
最佳答案
时间增加可能是因为并发 ==1 时,CPU 利用率不足,而 c==8 时 CPU 利用率超过 100%,无法满足所有请求。例如,抽象 CPU 每秒可以处理 1000 个操作,发送请求需要 50 个 CPU 操作,读取请求结果也需要 50 个 CPU 操作。当您的 RPS 为 5 时,您的 CPU 利用率为 50%,平均请求时间为 50 毫秒(发送请求)+ 请求时间 + 50 毫秒(读取请求)。但是,例如,当您的速度为 40 RPS(是 5 RPS 的 8 倍)时,您的 CPU 将被过度利用 400%,并且一些已完成的请求将等待解析,因此现在的平均请求时间为 50 ms + 请求时间 + CPU 等待时间 + 50 毫秒。
总而言之,我的建议是检查两个负载上的 CPU 利用率,并且可以肯定的是,要分析发送请求和解析响应所需的时间,CPU 可能是您的瓶颈。
关于python - Tornado AsyncHTTPClient 性能下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55346500/
根据一些谷歌搜索,我安装了以下错误处理程序。然而,似乎返回 http 500 的 python 异常并没有被这些东西捕获,尽管 404 是这样。通过我在下面的代码中留下的打印语句,我可以看到它没有命中
我刚刚意识到 WebSocketHandler.write_message() 返回一个 Future。我以前没有在我的函数中产生过这个函数: @tornado.gen.coroutine
这是我的 Tornado 文件:: from tornado.wsgi import WSGIContainer from tornado.ioloop import IOLoop from torn
class MainHandler(BaseHandler): @tornado.web.authenticated def get(self): self.rende
我正在尝试使用 AsyncHTTPTestCase 测试 Tornado .我想测试标有 @tornado.web.authenticated 注释的处理程序。因为此处理程序需要身份验证,所以我们必须
我正在使用 Tornado Web Server (版本 4.1)使用 Python 2.7 创建 REST Web 应用程序。我的请求处理程序之一 (web.RequestHandler) 使用多部
我想知道tornado 的内部工作流程,并且看过this article ,很好,但我就是想不通 ioloop.py里面有这样一个函数 def add_handler(self, fd, handle
如何遍历从 Python/Tornado 处理程序传递到 Tornado 模板的字典? 我试过 {% for key, value in statistics %}
我有一个 Tornado 后端,为 Angular 前端提供服务。更新数据库时,tornado api 不会获取更新的数据。它仅在我重新启动服务器后出现。有人可以帮我解决这个问题吗?我希望获取的数据能
我尝试使用自定义的 WSGIContainer 来处理异步操作: from tornado import httpserver, httpclient, ioloop, wsgi, gen @gen.
from tornado.web import RequestHandler class HelloWorldHandler(RequestHandler): def get(self):
Pylint 遇到 @tornado.web.authenticated 时崩溃 class Handler1(tornado.web.RequestHandler): def get(sel
经过 tornado.gen documentation有人可以帮我理解 tornado.gen.coroutine 和 tornado.gen.engine 之间的确切区别 最佳答案 正如 gen.
代码如下: from tornadoredis import Client from tornado.ioloop import IOLoop from tornado.gen import coro
我有一个 tornado.websocket.WebSocketHandler 的子类。在该类中,我有一个方法使用 Django ORM 从子类模型中获取用户:django.contrib.auth.
我是 ssl 之类的新手,我已经使用 openssl 生成了自签名证书。 openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days
我已经从 tornado 4.2 移动到 tornado 6.0.3,我得到了错误 AttributeError:模块“tornado.web”没有属性“异步” 根据 tornado v6 seems
我一直在关注此 ( https://developer.ibm.com/tutorials/se-distributed-apps-zeromq-part2/) 教程,以设置使用 CurveZMQ 加
我在使用tornado-celery整合tornado和celery时,出现错误:``` traceback (most recent call last): File "/usr/local/l
我正在使用 Tornado 与 twitter 等第三方进行身份验证。 我的登录处理程序看起来像这样 class AuthLoginHandler(BaseHandler, tornado.auth.
我是一名优秀的程序员,十分优秀!