0
s = requests.session()
s.get('http://foo.bar/')
s.post('http://foo.bar/')

The above code stops execution if the get or post method fail due to any reason. What I want it to do is that it should retry the get or post method until there is no exception.

Is there a way to put a wrapper on the requests.session() object so that all calls to its methods do a retry on any error?

I'm a newbie to Python so I don't know how to do this.

Elmo
  • 6,409
  • 16
  • 72
  • 140
  • 3
    Why not retrying in a loop? – bereal Sep 06 '14 at 12:29
  • 1
    Related: [Can I set max\_retries for requests.request?](http://stackoverflow.com/q/15431044) – Martijn Pieters Sep 06 '14 at 12:30
  • How can I retry for all my get and post calls? I just want a single retry loop – Elmo Sep 06 '14 at 12:38
  • @Zuck: what do you mean? You want to retry them *as a group*? Start from the beginning when any of them fails? – Martijn Pieters Sep 06 '14 at 12:39
  • It is rare to occure temporal failure for accessing web server. Whay do you think that retry are effective to solve your problem? What situation do you imagine that a failure without exception? – Fumu 7 Sep 06 '14 at 12:43
  • @MartijnPieters I have different functions which call the get and post methods on the `s` object many times in the code. Can I add the retry loop just once for all those somehow? – Elmo Sep 06 '14 at 12:43
  • @Zuck: yes, that is what the code in the other question **does**. It mounts a new HTTP adapter with a retry count. It'll retry connections up to that many times. *Each time you use the session*. – Martijn Pieters Sep 06 '14 at 12:44
  • @Fumu7 The server gets down & my code fails. – Elmo Sep 06 '14 at 12:44
  • @MartijnPieters I want infinite retries with a interval that I can set. For 1st retry- 10 seconds, 2nd - 20 seconds, 10th retry - 10 minutes so on. – Elmo Sep 06 '14 at 12:47
  • Also there are millions of subdomains. (something like *.herokuapp.com) That solution doesn't work. – Elmo Sep 06 '14 at 12:52
  • @Zuck: then you'd need to program that yourself; we can help you when you've come up with an attempt. A backing-off-retry feature is too broad to go code up in an answer. – Martijn Pieters Sep 06 '14 at 13:04
  • @Zuck: the prefix can be an empty string; e.g. apply to everything. Or use a protocol; the prefix `http://` applies to all HTTP connections. – Martijn Pieters Sep 06 '14 at 13:04
  • @Zuck: the generic name for what you want is a exponential backoff retry. A quick google turns up [`retrying`](https://pypi.python.org/pypi/retrying). – Martijn Pieters Sep 06 '14 at 13:06
  • @MartijnPieters Thank you so much. I think I'd do with the empty string now. – Elmo Sep 06 '14 at 13:07
  • @Zuck: I'd stay with `http://` in this case; by default there are two mounts, for `http://` and `https://`. – Martijn Pieters Sep 06 '14 at 13:10
  • @MartijnPieters I need both http and https. Do I need adapters for them each or will the empty string do? – Elmo Sep 06 '14 at 13:12
  • @Zuck: I *suspect* that separate adapters are registered to prevent HTTP connections being reused for HTTPS URLs. You can probably get away with the empty prefix, but perhaps registering two adapters is prudent here. – Martijn Pieters Sep 06 '14 at 13:16

0 Answers0