我最近开始从一些旧代码中发现错误,似乎无法追溯问题.因为它以前是工作的,我想可能是软件更新 destruct 了什么.我正在使用Python2.7和django runfcgi以及nginx.这是我的原始代码:
views.py个
DBNAME = "test"
DBIP = "localhost"
DBUSER = "django"
DBPASS = "password"
db = MySQLdb.connect(DBIP,DBUSER,DBPASS,DBNAME)
cursor = db.cursor()
def list(request):
statement = "SELECT item from table where selected = 1"
cursor.execute(statement)
results = cursor.fetchall()
我试过以下几种方法,但还是不起作用:
views.py个
class DB:
conn = None
DBNAME = "test"
DBIP = "localhost"
DBUSER = "django"
DBPASS = "password"
def connect(self):
self.conn = MySQLdb.connect(DBIP,DBUSER,DBPASS,DBNAME)
def cursor(self):
try:
return self.conn.cursor()
except (AttributeError, MySQLdb.OperationalError):
self.connect()
return self.conn.cursor()
db = DB()
cursor = db.cursor()
def list(request):
cursor = db.cursor()
statement = "SELECT item from table where selected = 1"
cursor.execute(statement)
results = cursor.fetchall()
目前,我唯一的解决办法是在每个使用MySQL的函数中执行MySQLdb.connect()
次.另外,我注意到在使用Django的manage.py runserver
时,我不会有这个问题,而nginx会抛出这些错误.我怀疑我的连接是否超时,因为list()
在服务器启动后的几秒钟内就被调用了.我正在使用的软件是否有任何更新会导致此中断/是否有修复此问题的方法?
编辑:我意识到我最近编写了一个中间件来守护一个函数,这就是问题的原因.不过,我想不出原因.这是中间件的代码
def process_request_handler(sender, **kwargs):
t = threading.Thread(target=dispatch.execute,
args=[kwargs['nodes'],kwargs['callback']],
kwargs={})
t.setDaemon(True)
t.start()
return
process_request.connect(process_request_handler)