我在Google App Engine上部署了一个使用Google API(Python)的项目.对任何API的每个请求都会建立数据库连接,执行过程并返回数据并关闭连接.我无法访问其显示的任何API
“由于超出了请求截止日期,进程终止.(错误代码123)”和“此请求导致为您的应用程序启动新进程,从而导致您的应用程序代码首次加载.比典型的应用程序请求更长,使用的cpu更多.“错误.
数据库也在云端(Google Cloud sql).我检查了900连接,超过150个实例,但没有处理api请求.这经常发生.所以我重新启动数据库服务器并再次部署API代码来解决此问题.问题是什么?如何永久解决这个问题?这是我的数据库连接的python代码: –
import logging
import traceback
import os
import MysqLdb
from warnings import filterwarnings
filterwarnings('ignore',category = MysqLdb.Warning)
class TalkWithDB:
def callQueries(self,query,req_args):
try:
if (os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
db = MysqLdb.connect(unix_socket = UNIX_SOCKET + INSTANCE_NAME,host = HOST,db = DB,user = USER,charset='utf8',use_unicode=True)
else:
db = MysqLdb.connect(host = HOST,port = PORT,passwd = PASSWORD,use_unicode=True)
cursor = db.cursor()
cursor.connection.autocommit(True)
try:
sql = query+str(req_args)
logging.info("QUERY = "+ sql )
cursor.execute(sql)
procedureResult = cursor.fetchall();
if str(procedureResult) == '()':
logging.info("Procedure Returned 0 Record")
procedureResult = []
procedureResult.append({0:"NoRecord",1:"Error"})
#procedureResult = (("NoRecord","Error",),)
elif procedureResult[0][0] == 'Session Expired'.encode(encoding='unicode-escape',errors='strict'):
procedureResult = []
procedureResult.append({0:"SessionExpired",1:"Error"})
except Exception,err:
logging.info("ConnectDB.py : - Error in Procedure Calling : " + traceback.format_exc())
#procedureResult = (('ProcedureCallError','Error',)
procedureResult = []
procedureResult.append({0:"ProcedureCallError",1:"Error"})
except Exception,err:
logging.info("Error In DataBase Connection : " + traceback.format_exc())
#procedureResult = (('DataBaseConnectionError',)
procedureResult = []
procedureResult.append({0:"DataBaseConnectionError",1:"Error"})
# disconnect from server
finally:
try:
cursor.close()
db.close()
except Exception,err:
logging.info("Error In Closing Connection : " + traceback.format_exc())
return procedureResult
最佳答案
两种可能的改进:
原文链接:https://www.f2er.com/mysql/433233.html>实例的启动代码可能需要很长时间,检查启动时间是多少,如果可能,请使用warmup requests来减少启动时间.由于增加空闲实例似乎有所帮助,因此启动时间可能会太长.
>更好的方法是在用户请求范围之外的Task Queue中调用外部服务(例如与Google日历交谈).这为您提供了10分钟的截止日期,而不是60秒的用户请求截止日期