[django1.6] Run Batch task error (2006, ' MySQL server has gone away ')

Source: Internet
Author: User
Tags ticket django website

There is a Django timed task that invokes the Django ORM to perform data processing on the database. in the interactive environment, the direct start of the Pyhton script is not a problem, when placed in a scheduled task, always appear (2006, ' MySQL server has gone away ')

error, and began to think that the timing frame is called outside the problem, but later think it is not reasonable, why directly in the shell call is right,The idea that some of the new properties of django1.6 's database connections, such as persistent connections, will have an impact, so Google is down.
I saw someone on the Django website mention something like a bug:https://code.djangoproject.com/ticket/21597


If you simply query this database error, a lot of it is to let you modify the configuration of MySQL, look at the final recommendations here.

If you hits this problem and don ' t want to understand what's going on, don ' t reopen this ticket, just does this:

    • RECOMMENDED Solution:close the connection with from django.db import connection; Connection.close () when you kno W that your program is going to be idle for a long time.
    • Crappy Solution:increase wait_timeout so it's longer than the maximum idle time of the Your program.

In this context, idle time is the time between the successive database queries.

This scenario says that the Django to MySQL database connection is closed, and then re-established to query, if you want to understand why, you can look at this discussion.


From django.db import Connection...def is_connection_usable ():    try:        connection.connection.ping ()    Except:        return False    else:        return True ... def do_work (): While    (True): # endless loop that keeps the Worke R going (Simplified)        if not is_connection_usable ():            connection.close ()        try:            do_a_bit_of_work ()        except:            logger.exception ("Something bad happened, trying again")            sleep (1)


The simple understanding is that django1.6 will automatically keep the connection, because I perform this task 24 hours a time, the first execution of Django and MySQL established a link a, the result of this task 10 minutes to complete, connect a start sleep, Django continue to maintain this connection.

After 23 hours and 50 minutes, the task started again, but since MSYQL's waittime default is 8 hours, connection A is dead (msyql unilaterally rescind the contract, Django is in the Dark), and Django uses connection A to connect to MySQL, Certainly get a result of a gone away.

All of us check to see if the connection is alive before each task, and if it doesn't, then Close,django automatically re-establish it, eliminating the error above.

This article is from the "Orangleliu Notebook" blog, reproduced please be sure to keep this source http://blog.csdn.net/orangleliu/article/details/41480417

Author Orangleliu using Attribution-NonCommercial-sharing protocol in the same way



[django1.6] Run Batch task error (2006, ' MySQL server has gone away ')

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.