简体   繁体   中英

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 .

# 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM