Reputation:
I'm working with proxies on request library for my project.Depending on the proxy used, each request takes different (unpredictable) time.
for proxy in proxyList:
r=requests.post(url, proxies=proxy, data=payload)
If a certain request takes more than certain time,I want to skip it and move on to the next.How can this be done?
EDIT : Using timeout parameter raises an exception.Of course,I can get my work done by Exception handling.But I wanted it in a more general sense..like what if I'm not talking about requests ..suppose there is statement whose time can't be pre-defined?! Is there any way to move on to next iteration?
Upvotes: 0
Views: 1224
Reputation: 1157
On Mac/UNIX systems, you can raise a custom exception after a certain period of time like this:
import signal
class TimeoutException(Exception): # custom exception
pass
def timeout_handler(signum, frame): # raises exception when signal sent
raise TimeoutException
# Makes it so that when SIGALRM signal sent, it calls the function timeout_handler, which raises your exception
signal.signal(signal.SIGALRM, timeout_handler)
# Start the timer. Once 5 seconds are over, a SIGALRM signal is sent.
for proxy in proxyList:
signal.alarm(5)
try:
r=requests.post(url, proxies=proxy, data=payload)
except TimeoutException:
pass
Basically, you're creating a custom exception that's raised when the after the time limit is up (i.e., the SIGALRM
is sent). You can of course tweak the time limit.
More simply in your case, you can use the timeout parameter, as far as I can tell. As the documentation says:
You can tell Requests to stop waiting for a response after a given number of seconds with the timeout parameter. Nearly all production code should use this parameter in nearly all requests. Failure to do so can cause your program to hang indefinitely [...]
so your code would end up looking like
for proxy in proxyList:
r=requests.post(url, proxies=proxy, data=payload, timeout=1)
(Timeout parameter is in seconds.)
Upvotes: 1
Reputation: 184081
Add timeout=10
(for example) to your post
call.
r = requests.post(url, proxies=proxy, data=payload, timeout=10)
You could have answered the question yourself with e.g. help(requests.post)
or by looking at the documentation.
Upvotes: 1