Django连接池试验
前置 接口执行慢,需要优化,经过cProfile分析,时间大部分耗在数据库连接查询上,故去深究了下django关于db连接的情况,发现django是不支持数据库连接池的,遂查询django关于为啥不支持连接池的事情,以及试用了下目前开源的一些连接池,做此记录。 这篇主要解决我的以下疑问: web请求过来的流程? wsgi server 和 wsgi application如何交互? django何时建立db连接的? django何时关闭db连接的? django长连接是怎么回事? django为何没有连接池? django如何实现连接池? 工具 cProfile 拿它主要看耗时在哪里,不做无用功。 cProfile是Python自带的性能分析的内置模块,使用起来很方便,一段话就看的明白 import cProfile import re cProfile.run('re.compile("foo|bar")') 输出如下 197 function calls (192 primitive calls) in 0.002 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.001 0.001 <string>