查到Django官網中:https://code.djangoproject.com/ticket/21597
這不是數據庫的問題。
If you hit this problem and don't want to understand what's going on, don't reopen this ticket, just do this:
RECOMMENDED SOLUTION: close the connection with
from django.db import connection
connection.close()
when you know 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 your program.
In this context, idle time is the time between two successive database queries.
def main(): q = Queue() p_1 = Process(target=func1, args=(selected, q)) p_2 = Process(target=func2, args=(request, q)) p_1.start() p_2.start() q.put(2) return |
執行過程: 1)main:開啓兩個進程;往隊列q裏寫入2;return 2)func1 和 func2 開始while循環, func1先從q中讀到2,於是執行func11, func11之後往q中寫3; func2在1秒後開始從q中讀到3,開始執行func22。 |
def func1(dc_id, q=None): while 1: value = q.get(True) time.sleep(1) if value == 2: func11(dc_id) # 操作數據庫 q.put(3) break |
def func2(request, q): while 1: time.sleep(1) value = q.get(True) if value == 3: func22(request) # 操作數據庫 break |
def func11(dc_id): if not is_connection_usable(): connection.close() ...操作數據庫... |
def func22(dc_id): if not is_connection_usable(): connection.close() ...操作數據庫... |
from django.db import connection def is_connection_usable(): try: print 'connection', connection.connection.ping() except: return False else: return True |