简体   繁体   English

如何限制python版Google App Engine中特定方法的允许执行时间?

[英]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.getdb.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.

相关问题 (如何)可以在Google App Engine python中的实体集合中添加方法? - (How) can you add methods to a collection of entities in Google App Engine python? Google App Engine如何限制Python? - How Google App Engine limit Python? 可以将自定义域指向特定的Google应用引擎版本域吗? - can a custom domain be pointed a specific google app engine version domain? 你可以解压缩并查看Google App引擎上的内容 - Python吗? - Can you Unzip and view content on Google App engine - Python? Python-Google App Engine-Django模板中的方法 - Python - Google App Engine - Methods in Django template Google App Engine,更改哪个python版本 - Google App Engine, Change which python version 您可以在Google App Engine中使用pydictionary吗? - can you use pydictionary in google app engine? 此资源不允许使用POST方法。 Google App Engine Python - The method POST is not allowed for this resource. Google App Engine Python 是什么在此 python 代码(谷歌应用引擎)中产生了“405 Method Not Allowed”? - What is producing “405 Method Not Allowed” in this python code (google app engine)? 在谷歌应用引擎开发服务器(python)上自动执行任务 - Automatic task execution on google app engine development server (python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM