未加星标

Cleaning up Django Persistent Database Connections

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

For the past year at work we have been utilizing more Kafka infrastructure for what typically would have been handled using celery workers. This has led to some interesting rediscoveries of Django ORM. While this example is used in a Kafka implementation, it is not dependent on it and can be used in any scenario where code is run outside of a typical Django view.

We started noticing long running idle connections against mysql. After tracing these back to our Kafka workers we soon realized that Django ORM deals with persistent database (DB) connections that are tightly coupled with the request/response cycle. The DB connections are released once a view generates the response. In the case of delayed workers, there is no request/response so Django ORM holds these connections open until MySQL forcefully closes them.

Since Celery is a gold standard for python delayed jobs and was born out of a Django implementation it must have already solved this problem. In fact it does a lot for trying to make Django/Django ORM work in the context of a worker process. Django Pre/Post Task run cleanup . Celery its a little more explicit which could be due to the age of the implementation. There is explicit cleanup that iterates through the list of available connections and closes them. In our case I think we can use some of Django's own healer functions to perform the cleanup.

Django has a wonderful helper function , close_old_connections , which will iterate through the list of functions and close if unusable or obsolete. Each Django ORM DB connection has a configurable life. This code will close the connection if the connection life exceeds the allowed life.

Now all that is left is how to run this function in the context of a Kafka worker. In this example lets assume our Kafka work is setup similarly to a Celery unit of work. We have a function that represents the type of work, this means we can wrap the function using a decorator.

from functools import wraps from django import db def cleanup_db_connections(func): @wraps(func) def wrapper(*args, **kwargs): try: r_val = func(*args, **kwargs) except db.OperationalError as e: db.close_old_connections() r_val = func(*args, **kwargs) finally: db.close_old_connections() return r_val return wrapper

Wrapper behavior in order

Attempt to run the wrapped function If Django ORM OperationalError is hit, close old connections and retry. This Error is raised in the case of a forcefully closed connection. Always close all old connections at the end of the function.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: DjangoKafkaSQLMySQL
分页:12
转载请注明
本文标题:Cleaning up Django Persistent Database Connections
本站链接:http://www.codesec.net/view/533376.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(32)