How to really implement a timeout in python? http://eventlet.net/doc/modules/timeout.html
The code looks like this:
#!/usr/bin/python import eventlet import time import sys import random while True: try: with eventlet.timeout.Timeout(1, False): print 'limited by timeout execution' while True: print '\r' + str(random.random()), sys.stdout.flush() eventlet.sleep(0) print ' Never printed Secret! ' except Exception as e: print ' Exception: ', e finally: print '' print ' Timeout reached ' print ''
The timeout will never reach. Where am I mistaken?
Ps I replaced:
time.sleep(0.1)
with:
eventlet.sleep(0)
Add False for an exception, now it works well:
with eventlet.timeout.Timeout(1):
change to:
with eventlet.timeout.Timeout(1, False):
But it only works with eventlet.sleep (0.1)
For example, this code is incorrect:
#!/usr/bin/python import eventlet import time start_time = time.time() data = 0 with eventlet.timeout.Timeout(1, False): while True: data +=1 print 'Catch data ', data, ' in ', time.time() - start_time
I just add sleep 0 seconds:
eventlet.sleep(0)
And it works like a charm.
Solved
source share