How can you limit the allowed execution time of specific methods in the python version of Google App Engine?

前端 未结 2 1696
春和景丽
春和景丽 2021-01-19 10:28

Since the signal module is not supported in the python version of Google App Engine, what is the simplest way to call a method and throw/catch an exception if the method doe

相关标签:
2条回答
  • 2021-01-19 11:16

    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.

    0 讨论(0)
  • 2021-01-19 11:18

    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.

    0 讨论(0)
提交回复
热议问题