0

I have a service and my code tries to connect to it with API:

import my_srvice_api

It haven't any mechanism for timeout and my code stays waiting if the service is not running, I want to try and after timeout raise the exception, how can I do that like this:

timeout = 3
try:
     obj = my_service_api.connect()
except TimeoutException as e:
     print("service my_service start")
RaminNietzsche
  • 2,683
  • 1
  • 20
  • 34
  • 1
    By only importing the service nothing will happen, unless you run your code in a function or by instantiating an object. If you want to set `timeout` for connection (which I think there might be some built-in methods out there, already) you need to put your connection in a loop and exit if it reaches to the limit. `try-except` is basically for handling the exceptions, for example if you want to handle the corresponding connection errors or etc. For a timeout you better to consider a loop with condition. – Mazdak Apr 05 '17 at 07:36
  • @Kasramvd your right, that's my fault. I changed it. – RaminNietzsche Apr 05 '17 at 07:40
  • try: r = requests.get(api_url, timeout=3) except requests.Timeout as err: print ('error') – GThamizh Apr 05 '17 at 07:40
  • 1
    Here is a similar question http://stackoverflow.com/questions/21965484/timeout-for-python-requests-get-entire-response – Mazdak Apr 05 '17 at 07:41
  • 1
    If `connect` is blocking I'm not sure what you can do (except modifying it and adding support for timeout). Another (ugly) possibility would be to call it in a separate thread and if that thread didn't end in _timeout_ seconds, forcibly end it. – CristiFati Apr 05 '17 at 07:43
  • you can try with this, http://stackoverflow.com/questions/24210792/checking-for-timeout-error-in-python – GThamizh Apr 05 '17 at 07:47
  • @GThamizh request not worked for my case – RaminNietzsche Apr 05 '17 at 07:50
  • @Kasramvd I used 'with eventlet.Timeout(3)` but not works – RaminNietzsche Apr 05 '17 at 07:54
  • What kind of service are we talking about here? Based on multiple recommendations of using `requests` it seems that it's a Web Service. Is that right? Maybe modifying `connect` (adding an extra argument specifying the timeout), is not such a terrible idea. But to be sure, I'd need to see its implementation. – CristiFati Apr 05 '17 at 07:54
  • 1
    Yes, I've browsed that page, but unfortunately nothing obvious popped up :( – CristiFati Apr 05 '17 at 08:49
  • @CristiFati I do that with thread :) Thanks to your attention – RaminNietzsche Apr 05 '17 at 09:22

1 Answers1

1

I add this decorator (base on this post) and its works. Thanks to all who attended to this question.

import threading
import sys
class TimeoutError(Exception): pass

def timelimit(timeout):
    def internal(function):
        def internal2(*args, **kw):
            class Calculator(threading.Thread):
                def __init__(self):
                    threading.Thread.__init__(self)

                def run(self):
                    try:
                        function(*args, **kw)
                    except:
                        pass

            c = Calculator()
            c.start()
            c.join(timeout)
            if c.isAlive():
                raise TimeoutError
        return internal2
    return internal

Then I add this decorator to my function:

@timelimit(2)
def time_out_api():
    obj = my_service_api.connect()

try:
    time_out_api()
except TimeoutError as e:
    print("service my_service start")
    exit(0)
RaminNietzsche
  • 2,683
  • 1
  • 20
  • 34