Hello,
obviously I don't understand a fundamental concept of timeouts in gevent/greenlets. Consider this code:
import gevent
import time
def t1():
with gevent.Timeout(seconds=1):
time.sleep(2)
return True
def t2():
def test():
time.sleep(2)
return True
g = gevent.spawn(test)
return g.get(timeout=1)
def main():
print t1()
print t2()
if __name__ == "__main__":
main()
I tested it on Linux and Windows with Python 2.7 and gevent 0.13.8. It prints two times "True". I expected it to raise a timeout exception in function t1 as well as in t2. In the docs it is stated that
"If a blocking function is called or an intense calculation is ongoing during
which no switches occur, :class:`Timeout` is powerless."
However, time.sleep() is no intense calculation :-) and the described behavior does not change if I split the time.sleeps into several smaller sleeps or add some other tasks (like a HTTP request to a remote resource) in between the sleeps.
What am I missing here? Looks like "no switches occur". I would be grateful if someone explained the architectural background leading to my observation. In which context do these timeout methods make sense? My goal actually is to just timeout-control various pretty common functions in my software.
I have to add that both, gevent and eventlet provide the with_timeout(seconds, function, *args, **kwds) method that by its nature suggests that it is applicable to any function. Obviously, this one also does not raise an exception for me in a very simple test case. Ironically, just FYI, the eventlet docs show this example:
data = with_timeout(30, urllib2.open, '
http://www.google.com/', timeout_value="")
Great, basically something like this is my use case. However, first of all, it does not work because urllib2 has no `open` attribute (the line obviously has never been tested). After adjusting this line, the timeout is -- of course -- never raised for me.
Thanks for your insights in advance,
Jan-Philip