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 does not return in less than 2 seconds?
-
I don't see documentation anywhere about it not being supported. Did you find anything, or just try using it and notice that when you import it you get an empty module? – Mu Mind Sep 22 '12 at 07:08
2 Answers
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.

- 8,282
- 25
- 21
-
Thanks. The RPC timeout feature should be helpful in many instances, but how can I throw a timeout exception if the method that will be calling will not be waiting on a query or datastore function to complete? As a contrived example, let's say the method that I call gets stuck in an infinite loop. – Chris Feb 01 '11 at 09:57
-
@Chris, You could use the time module (I linked to it in my answer) to do basic checking by storing the 'start time' and comparing that to the current time. Once you've gone past some limit, break from the loop (or return from the function, etc...). – Robert Kluin Feb 01 '11 at 17:27
-
Thanks again. I think fetching another url ends up being a practical approach. I can execute a method in the fetched url and just use the URLFetch deadline in the method doing the fetching. – Chris Feb 02 '11 at 11:10
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.

- 87,717
- 12
- 108
- 131