Answer the question
In order to leave comments, you need to log in
Python how to limit the execution time of a function?
Good afternoon!
I run into a problem with a function that either freezes or takes too long (sometimes):
response = requests.get(url)
parsed_body = html.fromstring(response.text)
title = parsed_body.xpath('//title/text()')
Answer the question
In order to leave comments, you need to log in
Set timeout docs.python-requests.org/en/master/user/quickstart...
timeout=5 can be added. In this example, 5 sec.
try:
response = requests.get(url, timeout=5)
parsed_body = html.fromstring(response.text)
title = parsed_body.xpath('//title/text()')
except:
print("time out")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question