Skip to content

Instantly share code, notes, and snippets.

@schmir
Created May 9, 2012 15:02
Show Gist options
  • Save schmir/2645197 to your computer and use it in GitHub Desktop.
Save schmir/2645197 to your computer and use it in GitHub Desktop.
import gevent
from gevent.hub import get_hub
lst = []
def _on_async():
oldlen = len(lst)
for fun, args, kw in lst[:]:
gevent.spawn(fun, *args, **kw)
del lst[:oldlen]
def call_in_loop(fun, *args, **kw):
lst.append((fun, args, kw))
_async.send()
_async = get_hub().loop.async(ref=False)
_async.start(_on_async)
def show(msg):
print msg
def doit():
import time
for i in range(10):
time.sleep(0.2)
call_in_loop(show, i)
import threading
t = threading.Thread(target=doit)
t.start()
gevent.sleep(10)
t.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment