Python 并测量以毫秒为单位的时间,如果满足阈值则中止
我使用 nginx、web.py、fastcgi 和 redis 作为我的堆栈。
收到发布请求后,我有 120 毫秒的时间返回响应,因此我需要始终测量响应,如果即将接近阈值,我需要中止并返回 False。无论真假,我都不会受到惩罚,只有当我超过 120 阈值时,这是一个例外。我预计 10-50K qps。
我可以简单地执行 if 条件,但我担心一个长时间运行的过程,并且我必须等待结束才能发现它花了很长时间,例如我正在使用管道的 redis 调用。例如
start = time.time()
r.get()
end = time.time - start
if end>115 then return False
,如果 r.get() 花费的时间太长,例如 130 毫秒,那么我就会受到惩罚。
在监控进程不占用太多资源的情况下监控时间并发送中止信号的python最佳实践是什么?我要启动一个线程吗?
我使用超时吗?如果是的话那么在MS范围内如何?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对于 Python 中的 装饰器 来说是一个很好的用途。有人已经编写了一个 @timeout 装饰器,你可以这样使用
:超时,而不是返回“稍后”,它将生成异常:
但是,由于实现使用
signal.alarm()
,它只接受int
,因此您无法指定几分之一秒,只有整秒。由于看起来您想使用毫秒,因此您可以调整此装饰器以使用signal.setitimer()
。或者,如果您足够勇敢,更好的解决方案是按照某人实现 setitimer 功能的方式提交补丁 到signal
模块以支持ualarm()
,它提供微秒分辨率,并且比setitimer()
使用更简单。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 thesignal
module to supportualarm()
which provides microsecond resolution and is simpler to use thansetitimer()
.