python MySQLDB query timeout

后端 未结 6 1826
误落风尘
误落风尘 2020-12-24 09:45

I\'m trying to enforce a time limit on queries in python MySQLDB. I have a situation where I have no control over the queries, but need to ensure that they do not run over a

6条回答
  •  隐瞒了意图╮
    2020-12-24 10:08

    Use adbapi. It allows you to do a db call asynchronously.

    from twisted.internet import reactor
    from twisted.enterprise import adbapi
    
    def bogusQuery():
        return dbpool.runQuery("SELECT SLEEP(10)")
    
    def printResult(l):
        # function that would be called if it didn't time out
        for item in l:
            print item
    
    def handle_timeout():
        # function that will be called when it timeout
        reactor.stop()
    
    dbpool = adbapi.ConnectionPool("MySQLdb", user="me", password="myself", host="localhost", database="async")
    bogusQuery().addCallback(printResult)
    reactor.callLater(4, handle_timeout)
    reactor.run()
    

提交回复
热议问题