Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-(2006年,MySQL服务器已经消失了';)_Python_Mysql - Fatal编程技术网

Python-(2006年,MySQL服务器已经消失了';)

Python-(2006年,MySQL服务器已经消失了';),python,mysql,Python,Mysql,我熟悉这个错误,因此我将交互超时和等待超时增加到X秒。因此,每当MySQL空闲超过X秒时,它就会重新连接(根据我的代码) 下面是代码 from filename import getconnection, DButil CONNECTION = getconnection() def user(): try: global CONNECTION if CONNECTION is None: CONNECTION = getcon

我熟悉这个错误,因此我将
交互超时
等待超时
增加到X秒。因此,每当MySQL空闲超过X秒时,它就会重新连接(根据我的代码)

下面是代码

from filename import getconnection, DButil

CONNECTION = getconnection()

def user():
    try:
        global CONNECTION
        if CONNECTION is None:
            CONNECTION = getconnection()
    except Exception,e:
        print e


    dbutil = DButil(CONNECTION)

    try:
        dbutil.data_exist(loginfo)
    except Exception,e:
        print e
filename.py

CONNECTION = None

def getconnection(recreate=True, db_name='db_name'):
    ''' Get a connection, if not available, make it '''
    global CONNECTION
    if recreate or CONNECTION is None:
        if db_name == 'db_name':
            CONNECTION = makeconnection()
    return CONNECTION



class DButil(object):
    '''Low level utiliity arrond db '''
    def __init__(self, _connection):
        self.connection = _connection


    def data_exist(self,data_exist_query):
        with self.connection:
            print self.connection # <_mysql.connection open to '127.0.0.1' at 1a587b0>
            cur = self.connection.cursor()
            print cur # <MySQLdb.cursors.Cursor object at 0x1a22710> cur
            cur.execute(data_exist_query)
            rows = cur.fetchall()
            print rows #  (2006, 'MySQL server has gone away')
            if len(rows) >= 1 :
                return True
            else :
                return False
连接=无
def getconnection(recreate=True,db_name='db_name'):
''获取连接,如果不可用,请创建''
全局连接
如果“重新创建”或“连接”为“无”:
如果db_name==“db_name”:
CONNECTION=makeconnection()
回路连接
类DButil(对象):
“配电盘附近的低水平实用性”
定义初始化(自连接):
self.connection=\u连接
def数据存在(自身、数据存在查询):
通过自我连接:
打印自连接
cur=self.connection.cursor()
打印电流#电流
当前执行(数据存在查询)
rows=cur.fetchall()
打印行#(2006年,“MySQL服务器消失了”)
如果len(行)>=1:
返回真值
其他:
返回错误
我得到的是正在建立连接,并且正在打开,但是当我执行
rows=cur.fetchall()
时,它抛出了
(2006,“MySQL服务器消失了”)
。为什么会有这样的行为


当我重新连接时,即重新加载uwsgi
uwsgi
,它工作正常,因为MySQL连接再次建立。因为,当服务器空闲超过X秒时,我也再次创建了连接,但它仍然会抛出错误。为什么?

完全是OT,但你的try/except子句比无用的更有价值…@brunodesshuilliers-我更担心这个错误。:)