我们使用Django来运行模拟仿真。代码作为cronscript运行(不通过wsgi)并注册模拟是否成功运行。 在模型仿真开始时,状态对象创建时的状态为RUNNING。在模型仿真结束时,此状态对象更新为SUCCESS或FAILED。 (伪)代码看起来是这样的:当modelsimulation需要很长的时间(数小时模拟来天是可能的)'MySQL server has gone away'with longlasting processes
def mainRoutine():
myStatusObject = createStatusObject()
try:
runModelSimulation() # this runs the modelsimulation
except:
updateStatus(myStatusObject,'FAILED')
updateStatus(myStatusObject,'SUCCESS')
def createStatusObject():
myStatusObject = models.StatusObject()
myStatusObject.task = 'somename'
myStatusObject.status = 'RUNNING'
myStatusObject.save()
return myStatusObject
def updateStatus(myStatusObject, newstatus):
myStatusObject.status = newstatus
myStatusObject.save()
的问题开始。然后,MySQL数据库发生连接错误,错误“MySQL服务器已经消失”。我从其他帖子了解到,这与链接到数据库的一些连接(可以通过摆弄MySQL的服务器端配置来解决)。
目前,我通过传递status对象的ID而不是status对象实例来找到解决方法。在updateStatus子例程中,我使用get(ID = thisID)在更新之前检索正确的statusObject实例。这不会导致超时。
def updateStatus(myStatusObjectID, newstatus):
myStatusObject = StatusObject.objects.get(id=myStatusObjectID)
myStatusObject.status = newstatus
myStatusObject.save()
问题解决了?但是,我们有其他类似于statusobject的对象,可能会遇到模拟问题。所以我想了解为什么这个连接保持开放。传递StatusObject实例和传递与.get()相结合的StatusObject ID有什么区别?在什么时候建立了连接,以及如何防止此连接继续打开?我们可以告诉django在.save()之后关闭连接,并在实例的下一次更新时重新打开连接。 另外,因为传递StatusObject实例更容易,而不是基于它的属性进行搜索并重新打开它。
好的,工作。谢谢 –