Skip to content

Instantly share code, notes, and snippets.

@insanemainframe
Last active January 4, 2016 05:59
Show Gist options
  • Save insanemainframe/8578760 to your computer and use it in GitHub Desktop.
Save insanemainframe/8578760 to your computer and use it in GitHub Desktop.
class TimeoutContext(object):
u"""
Контекст устанавливающий таймаут на операции с сокетами
>>> with TimeoutContext(14.88):
... urlopen('http://google.com').read()
Traceback (most recent call last)
...
timeout: timed out
"""
def __init__(self, timeout=None):
self.timeout = timeout
def __enter__(self):
self.old_timeout = getdefaulttimeout()
setdefaulttimeout(self.timeout)
def __exit__(self, *exc_info):
setdefaulttimeout(self.old_timeout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment