3

I have this code where I am executing a sql statement using pymysql library:

try:
    conn = pymysql.connect(db_endpoint, user=db_username,
                           passwd=db_password, db=db_name, connect_timeout=5)

    cur = conn.cursor()

    result = cur.execute(sql)
    resultSet1 = cur.fetchall()


except:
    print('ERROR: Unexpected error: Could not connect to MySql instance.')
    logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
    sys.exit()

Now what I want to do is track how long this query takes to execute and if it exceeds say more than 2 minutes then stop the execution and exit with a print message for timeout. I know I can use default_timer() to initiate a timer in python but I am unable to make a continuous check to see as soon as it hits 2 minutes it should stop the execution. How can I do that?

cmaher
  • 5,100
  • 1
  • 22
  • 34
user2916886
  • 847
  • 2
  • 16
  • 35
  • Create a decorator that will timeout after x amount of seconds.. [check out this answer](https://stackoverflow.com/a/2282656/4889267) and then create wrap your methods in the timeout decorator – Alan Kavanagh Oct 23 '17 at 18:27
  • 2
    Possible duplicate of [Timeout on a function call](https://stackoverflow.com/questions/492519/timeout-on-a-function-call) – Joe Iddon Oct 23 '17 at 18:28

1 Answers1

0

you can run a daemon process and check the time

import time
import multiprocessing as mp

class TimeoutChecker(object):
  def __init__(self, func):
    self.waiting_time = 120
    self._worker_func = func 
    self._queue = mp.Queue()
    self._process = mp.Process()

  def cancel(self):
    if self._process.is_alive():
      self._process.terminate()

  def __call__(self):
    self.cancel()
    self._queue = mp.Queue(1)
    self._process = mp.Process(target=self._worker_func)
    self._process.daemon = True
    self._process.start()
    self.start = time.time()

  def _is_ready(self):
    while self._queue.empty():
      if self.waiting_time < (time.time() - self.start):
        self.cancel()
        return False
    return True

  def value(self):
    if self._is_ready():
      val = self._queue.get()
      return val
    return 'time out'

checker = TimeoutChecker(your function)
checker.value()
galaxyan
  • 5,944
  • 2
  • 19
  • 43