Possible Duplicate:
Timeout on a Python function call
I want to implement that when the function took more than 90 seconds to complete it should return immediately when timeout. Is there any way to achieve that?
def abc(string):
import re
if re.match('some_pattern', string):
return True
else:
return False
abc('some string to match')
Edited
Please download this test file. I have created a thread class and raise an exception within thread if timeout error occur. But thread is still alive because it prints i am still alive :)
even after exception. Why an exception does not force the thread to stop??
So, in your application code, you can use the decorator like so: from timeout import timeout # Timeout a long running function with the default expiry of 10 seconds. @timeout def long_running_function1(): ... # Timeout after 5 seconds @timeout(5) def long_running_function2(): ...
Then it's as simple as this to timeout a test or any function you like: @timeout(5.0) # if execution takes longer than 5 seconds, raise a TimeoutError def test_base_regression(self): ... Be careful since this does not terminate the function after timeout is reached!
There is no default timeout for Python requests, unless explicitly set using the timeout parameter. How do you set a timeout for requests made in Python? You set a timeout (in seconds) using the timeout= parameter when making HTTP requests in the Python requests library.
The reason why a timeout is being generated is because the program is trying to write 50000 documents at once, the Python SDK has a default timeout value of 2.5 seconds. There are a few options here: Increase the timeout.
I've edited my post to use jcollado's idea which is simpler.
The multiprocessing.Process.join method has a timeout argument which you can use like this:
import multiprocessing as mp
import time
import logging
import re
logger = logging.getLogger(__name__)
def abc(string, result, wait = 0):
time.sleep(wait)
result.put(bool(re.match('some_pattern', string)))
if __name__ == '__main__':
logging.basicConfig(level = logging.DEBUG,
format = '%(asctime)s: %(message)s',
datefmt = '%H:%M:%S', )
result = mp.Queue()
proc = mp.Process(target = abc, args = ('some_pattern to match', result))
proc.start()
proc.join(timeout = 5)
if proc.is_alive():
proc.terminate()
else:
logger.info(result.get())
proc = mp.Process(target = abc, args = ('some string to match', result, 20))
proc.start()
proc.join(timeout = 5)
if proc.is_alive():
logger.info('Timed out')
proc.terminate()
else:
logger.info(result.get())
yields
12:07:59: True
12:08:04: Timed out
Note that you get the "Timed out" message in 5 seconds, even though abc('some string',20)
would have taken around 20 seconds to complete.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With