[英]How can you limit the allowed execution time of specific methods in the python version of Google App Engine?
由于python版本的Google App Engine不支持信号模块,如果方法在不到2秒内没有返回,调用方法和抛出/捕获异常的最简单方法是什么?
If you are talking about RPC calls, such as the datastore, you can create an RPC with a deadline (see create_rpc
), pass the RPC to your datastore function ( db.get , db.put , etc...), then catch DeadlineExceededErrors
. 如果您正在讨论RPC调用(例如数据存储区),则可以使用截止日期创建RPC(请参阅
create_rpc
),将RPC传递给数据存储区功能( db.get , db.put等等),然后捕获DeadlineExceededErrors
。
# Set a five-second timeout
rpc = db.create_rpc(deadline=5)
# A query:
query = YourModel.all().fetch(100, rpc=rpc)
The URLFetch fetch function also takes a deadline parameter. URLFetch 获取功能也需要截止日期参数。
For your own code you could implement checking yourself, see the time module. 对于您自己的代码,您可以自己实现检查,请参阅时间模块。
In loops, you can store the time the loop started and check how long it's been going on each iteration. 在循环中,您可以存储循环开始的时间并检查每次迭代的持续时间。
If you're not in a loop, things are a bit trickier. 如果你不在循环中,事情会有点棘手。 You could add the time-checking bit every few lines of code.
您可以每隔几行代码添加时间检查位。 This, of course, makes for really ugly code, but without the ability to spawn threads that could run a timer in the background and interrupt the running code, there's not much of a way around it.
当然,这会产生非常丑陋的代码,但是如果没有能够生成可以在后台运行定时器并中断正在运行的代码的线程,那么就没有太多方法了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.