Created
April 20, 2011 17:42
-
-
Save denik/932102 to your computer and use it in GitHub Desktop.
make non-yielding functions (a little bit) yielding, so that gevent.Timeout somewhat works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import signal | |
import gevent | |
from gevent import get_hub | |
import traceback | |
def alarm_handler(*args): | |
signal.alarm(1) | |
if gevent.getcurrent() is not get_hub(): | |
get_hub().loop.update_now() | |
gevent.sleep(0) | |
signal.signal(signal.SIGALRM, alarm_handler) | |
signal.alarm(1) | |
import time | |
start = time.time() | |
with gevent.Timeout(1, False): | |
print 'this is yielding code' | |
gevent.sleep(5) | |
print 'interrupted after %s seconds' % (time.time() - start) | |
start = time.time() | |
with gevent.Timeout(1, False): | |
print 'this is non- yielding code' | |
while True: | |
x = 1 + 1 | |
print 'interrupted after %s seconds' % (time.time() - start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment