Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is something that is really nice in gevent. Under the hood it's doing something similar to what the article says - every time you make a blocking call (in gevent, this means yielding to the event loop until your event occurs), you might have a gevent.Timeout raised.

Since gevent is generally used by monkey-patching all standard IO, most code doesn't even need to be aware of this feature - it just treats a timeout as an unhandled exception.

From the user's perspective, it can be used simply as a context manager that cancels the timeout on exit from the block:

    with gevent.Timeout(10):
      requests.get(...)
By default this will cause the Timeout to be raised, which you can then catch and handle. As a shorthand, you can also give it an option to suppress the exception, effectively jumping to the end of the with block upon timeout:

    response = None
    with gevent.Timeout(10, False):
      response = requests.get(...)
    if response is None:
      # handle timeout


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: