I am using nginx, web.py, fastcgi, and redis as my stack.
Upon a post request I have 120 ms to return a response so I need to always measure the response and if about to approach the threshold I need to abort and return False. I dont get punished if true or false, only if I exceed the 120 threshold where its an exception. I expect 10-50K qps.
I can simply do if conditions but I am concerned about a long running process and I will have to wait to end before I find out it took to long e.g. a redis call where I am using pipelining. For example
start = time.time()
r.get()
end = time.time - start
if end>115 then return False
if r.get() takes too e.g. 130ms long then I get punished.
What is python best practice to monitor time and send an abort signal without the monitoring process not taking up too much resources? Do I fire up a thread?
Do I use a timeout? if So then how in the MS range?
Thanks
This is a good use for a decorator in Python. Somebody has already written a @timeout decorator that you can use this way:
Since sleep time > timeout, instead of returning ‘later’ it will generation an exception:
However because the implementation uses
signal.alarm()which only takes anint, you can’t specify a fraction of a second, only whole seconds. Since it looks like you want to use milliseconds you could adapt this decorator to usesignal.setitimer(). Or an even better solution if you’re intrepid enough is to submit a patch the way someone did to implement setitimer functionality to thesignalmodule to supportualarm()which provides microsecond resolution and is simpler to use thansetitimer().