Skip to content

Instantly share code, notes, and snippets.

@mithrandi
Created March 13, 2015 14:11
Show Gist options
  • Save mithrandi/8922bd4fbff4b7263d4b to your computer and use it in GitHub Desktop.
Save mithrandi/8922bd4fbff4b7263d4b to your computer and use it in GitHub Desktop.
Agent timeout example
def fetch(..., clock=None):
if clock is None:
from twisted.internet import reactor as clock
def handleResponse(response):
# ...
d = agent.request(method, url, Headers(h), body)
call = clock.callLater(timeout, d.cancel)
def cancelTimeout(result):
if call.active():
call.cancel()
return result
d.addCallback(handleResponse)
d.addBoth(cancelTimeout)
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment