I have a lengthy Python process that uses Django ORM. It looks something like this:
import django
from django.db import connection
from my_app import models
def my_process():
django.setup()
while (True):
do_stuff()
connection.close()
models.MyDjangoModel().save()
Sometimes it do_stufftakes a very long time, and at this moment I encountered an error when my connection to MySql was disconnected because the database server killed the connection as idle. Adding a line connection.close()causes django to get a new connection every time and fixes this problem. (See https://code.djangoproject.com/ticket/21597 ).
, django.test.TestCase, connection.close , django TestCase , a django.db.transaction.TransactionManagementError.
, , CONN_MAX_AGE connection.close_if_unusable_or_obsolete , autocommit True : False, close_if_unusable_or_obsolete (https://github.com/django/django/blob/master/django/db/backends/base/base.py#L497).
, connection.close , , .
django, ?