refactor timeout logic to support old version of python.