Skip to content

Instantly share code, notes, and snippets.

@amotoki
Created January 29, 2014 08:53
Show Gist options
  • Save amotoki/8684114 to your computer and use it in GitHub Desktop.
Save amotoki/8684114 to your computer and use it in GitHub Desktop.
import threading
import time
import eventlet
import eventlet.corolocal
# NOTE(rpodolyaka): threading is patched here
eventlet.monkey_patch()
# NOTE(rpodolyaka): meant to be 'thread local storage' for green threads
# currently we use like this
#local = eventlet.corolocal.local
# but in fact it must be used like this
local = eventlet.corolocal.local()
def f():
local.id = eventlet.corolocal.get_ident() # is unique for each green thread
while True:
print 'Thread %d -> local storage value = %d' % (eventlet.corolocal.get_ident(), local.id)
time.sleep(2) # yield
for i in xrange(3):
t = threading.Thread(target=f) # these are green threads
t.start()
while True:
#time.sleep(0)
print '-------------'
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment