Python 2.7: Request with retries does not function - python-2.7

I am testing with a url that will return 503 and would want the script to request the URL with a backoff_factor of 1 with max _retries function. Base on my 8 times retries, theoretically the request was supposed to spend at least 64 s for the request but it just straight away print the response 503 and I do not think it actually retries for 8 times. I even increase backoff_factor to 20 and still instantly print out response 503
from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
import requests
url = 'http://httpstat.us/503'
s = requests.Session()
retries = Retry(total=8, backoff_factor=1, status_forcelist=[ 502, 503, 504 ])
s.mount('http://', HTTPAdapter(max_retries=retries))
response = s.get(url)
print response
I have been checking other post and try with s.mount('http://', requests.adapters.HTTPAdapter(max_retries=retries)) with similar result.
How to let the script really retries 8 times and confirm it actually retries?
Please advise if the solution works for HTTP and https
When I call retries
>>> retries
Retry(total=8, connect=None, read=None, redirect=None)
Is the input correct?
I base on https://stackoverflow.com/a/35636367/5730859 solution and but it does not seems to retry. Can anyone help?

Updating to newest comments. Instead of using that urllib3 function, use your own:
>>> def test():
return requests.get("http://httpstat.us/503").status_code
>>> def main():
total_retries = 0
code = test()
while code == 503 or 502 or 504:
total_retries+=1
if total_retries > 8:
print("8 retries hit")
break
code = test()
>>> main()
8 retries hit
>>>
This will stop once it hits 8 retries with the 503, 504, 502 code(s).

Related

pub/sub synchronous pull w deadline

from google.api_core import retry
from google.cloud import pubsub_v1
NUM_MESSAGES = 1
with subscriber:
response = subscriber.pull(request={"subscription": subscription_path, "max_messages": NUM_MESSAGES},retry=retry.Retry(deadline=120),)
Requirement is to wait for X (120) seconds for the message to arrive in subscription, read it & validate some fields in it. Somehow, above code snippet was NOT working w.r.t deadline . It's not waiting for 120 seconds and timing out much before . what am I missing !?
ref: pub/sub synchronous pull w deadline
As Guillaume mentioned, the timeout argument should be used how long the Pull request is open for. The default timeout looks to be 60 seconds.
So, you could write:
response = subscriber.pull(request={"subscription": subscription_path, "max_messages": NUM_MESSAGES},timeout=120.0,)

"Rate of traffic exceeds capacity" error on Google Cloud VertexAI but only sending a single prediction request

As In the title. Exact response:
{
"error": {
"code": 429,
"message": "Rate of traffic exceeds capacity. Ramp your traffic up more slowly. endpoint_id: <My Endpoint>, deployed_model_id: <My model>.",
"status": "RESOURCE_EXHAUSTED"
}
I send a single prediction request which consists of an instance of 1 string. The model is a pipeline of a custom tfidf vectorizer and logistic regression. I timed the loading time: ~0.5s, prediction time < 0.01s.
I can confirm through logs that the prediction is executed successfully but for some reason this is the response I get. Any ideas?
Few things to consider:
Allow your prediction service to serve using multiple workers
Increase your number of replicas in Vertex or set your machine types to stronger types as long as you gain improvement
However, there's something worth doing first in the client side assuming most of your prediction calls go through successfully and it is not that frequent that the service is unavailable,
Configure your prediction client to use Retry (exponential backoff):
from google.api_core.retry import Retry, if_exception_type
import requests.exceptions
from google.auth import exceptions as auth_exceptions
from google.api_core import exceptions
if_error_retriable = if_exception_type(
exceptions.GatewayTimeout,
exceptions.TooManyRequests,
exceptions.ResourceExhausted,
exceptions.ServiceUnavailable,
exceptions.DeadlineExceeded,
requests.exceptions.ConnectionError, # The last three might be an overkill
requests.exceptions.ChunkedEncodingError,
auth_exceptions.TransportError,
)
def _get_retry_arg(settings: PredictionClientSettings):
return Retry(
predicate=if_error_retriable,
initial=1.0, # Initial delay
maximum=4.0, # Maximum delay
multiplier=2.0, # Delay's multiplier
deadline=9.0, # After 9 secs it won't try again and it will throw an exception
)
def predict_custom_trained_model_sample(
project: str,
endpoint_id: str,
instance_dict: Dict,
location: str = "us-central1",
api_endpoint: str = "us-central1-aiplatform.googleapis.com",
):
...
response = await client.predict(
endpoint=endpoint,
instances=instances,
parameters=parameters,
timeout=SOME_VALUE_IN_SEC,
retry=_get_retry_arg(),
)

Python requests unable to access https site

I run this code:
import requests
loginurl = 'https://auth.cbssports.com/login/index'
try:
response = requests.get(loginurl)
except requests.exceptions.ConnectionError as e:
print "Login URL is BAD"
response = requests.get(loginurl)
It consistently returns this error:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='auth.cbssports.com', port=443): Max retries exceeded with url: /login/index (Caused by <class 'socket.error'>: [Errno 10054] An existing connection was forcibly closed by the remote host)
I am able to access this url manually. I can't figure out why Python can't. Is there a way to solve this?

I am using httplib2 and had read that httplib2 handles caching automatically but when I execute 'fromcache' on the response object, it returns false

This is my code
import httplib2
httplib2.debuglevel = 1
h = httplib2.Http(".cache")
response, content = h.request('http://diveintopython3.org/examples/feed.xml')
print(response.fromcache)
The link(http://diveintopython3.org/examples/feed.xml) I was using in my code is inactive now that why I was facing such issue.
Using a resource that, at time of writing, exists and declares itself cachable:
#!/usr/bin/python3
import httplib2
h = httplib2.Http('cache')
response, content = h.request('https://example.com/')
print(response.status)
print(response.fromcache)
response, content = h.request('https://example.com/')
print(response.status)
print(response.fromcache)
Gives:
200
False
200
True
on the first run, indicating that both requests were successful, and that the second was served from the cache. Running it again gives:
200
True
200
True
as the cache is persisted on disk from the previous run.

Django HTTP Response object for GAE Cron

I am doing this using Django / GAE / Python environment:
cron:
#run events every 12 hours
and
def events(request):
# read all records
# Do some processing on a few records
return http.HTTPResponseGone('Some Records are modified' )
Result in production :
Job runs on time with 'failed' message
However, it has done the job exactly on the datastore as required
No error log entry seen
Dev : No errors ; returns the message 'Some Records are modified'
Is it possible to avoid HTTP Response returned ? There is no need for HTTPResponse for me, however, I have kept this as Dev server testing fails in its absence. Can some one
help me to make the code clean?
Gone is error 410. You should return 200 Success if the operation succeeds. When you return HttpResponse, the default status is 200.