python MySQLDB query timeout

后端 未结 6 1953
误落风尘
误落风尘 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:03

    Why do I not get the signal until after execute finishes?

    The query is executed through a C function, which blocks the Python VM from executing until it returns.

    Is there another reliable way to limit query execution time?

    This is (IMO) a really ugly solution, but it does work. You could run the query in a separate process (either via fork() or the multiprocessing module). Run the alarm timer in your main process, and when you receive it, send a SIGINT or SIGKILL to the child process. If you use multiprocessing, you can use the Process.terminate() method.

提交回复
热议问题