Skip to content

Instantly share code, notes, and snippets.

@snaury
Created March 27, 2011 00:05
Show Gist options
  • Save snaury/888764 to your computer and use it in GitHub Desktop.
Save snaury/888764 to your computer and use it in GitHub Desktop.
How to take advantage of greenlet gc support in gevent
#!/Users/dragonfox/vpython/bin/python
import gc
import sys
from greenlet import getcurrent, GreenletExit
from gevent import spawn, spawn_later
from gevent.hub import get_hub
from gevent.greenlet import Greenlet
def run():
self = getcurrent()
try:
self._start_event = None
try:
result = self._run(*self.args, **self.kwargs)
except GreenletExit:
raise
except:
self._report_error(sys.exc_info())
return
self._report_result(result)
finally:
self = getcurrent()
self.__dict__.pop('_run', None)
self.__dict__.pop('args', None)
self.__dict__.pop('kwargs', None)
Greenlet.run = staticmethod(run)
def myfunc():
try:
get_hub().switch() # leak ourselves
finally:
print "Finally called!!"
def collect():
print "Collecting..."
gc.collect()
print "Collecting done"
spawn(myfunc)
spawn_later(1.0, collect).join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment