Skip to content

Instantly share code, notes, and snippets.

@liushuaikobe
Forked from ghoseb/gevent_example.py
Created November 16, 2013 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liushuaikobe/7495369 to your computer and use it in GitHub Desktop.
Save liushuaikobe/7495369 to your computer and use it in GitHub Desktop.
from gevent import monkey; monkey.patch_all()
import gevent
import gevent.greenlet
from functools import partial
from random import random
import urllib
import urllib2
def on_exception(fun, greenlet):
"""Receiver in case of an error
"""
print "Oops! Restarting..."
fun()
def fetch_url(url):
"""Fetch an URL, sleep for some time and repeat. Die occasionally to emulate failure
"""
while True:
r = random()
if r < 0.3:
print "Raising an exception!"
raise Exception("Time to die :(")
data = urllib2.urlopen(url).read()
print "Fetched %s..." % url
gevent.sleep(r)
def main(urls):
gs = [gevent.spawn(fetch_url, url) for url in urls]
x = [g.link_exception(partial(on_exception, partial(main, urls))) for g in gs]
gevent.joinall(gs)
if __name__ == '__main__':
print main(["http://google.co.il", "http://google.com"])
@liushuaikobe
Copy link
Author

x = [g.link_exception(partial(on_exception, main(urls)))) for g in gs]

Why not do like this?
Cause we have been sure that the main function won't have more args.

Sorry for my ignorance.
main(urls) is the result of the invoking of the main function rather than a callable object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment