Last active
February 9, 2018 02:48
-
-
Save allenyang79/4fed2e5663ab18f05bf3490c93077060 to your computer and use it in GitHub Desktop.
test greenlet link
This file contains hidden or 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 mock | |
import gevent | |
import random | |
def do_job(name='noname', sleep=10): | |
print "begin...%s" % sleep | |
for i in range(sleep): | |
gevent.sleep(i) | |
print "sleep...%s" % i | |
if random.randint(0, 3) == 0: | |
print "oops" | |
print 10 / 0 | |
print "done" | |
return "oops" | |
glet = gevent.spawn(do_job, sleep=1) | |
@glet.link | |
def callback(glet, *args, **kw): | |
"""when glet finished, called, even if any exception""" | |
print "callback called" | |
print glet, args, kw | |
# or | |
# glet.link(callback) | |
@glet.link_value | |
def callback_with_value(glet, *args, **kw): | |
"""when glet finished but no exception, called""" | |
print "callback_with_value called" | |
print glet, args, kw | |
gevent.sleep(5) | |
print "done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment