1 """Implements the standard threading module, using greenthreads."""
2 from eventlet import patcher
3 from eventlet.green import thread
4 from eventlet.green import time
5 from eventlet.support import greenlets as greenlet
7 __patched__ = ['_start_new_thread', '_allocate_lock', '_get_ident', '_sleep',
8 'local', 'stack_size', 'Lock', 'currentThread',
9 'current_thread', '_after_fork', '_shutdown']
11 __orig_threading = patcher.original('threading')
12 __threadlocal = __orig_threading.local()
27 class _GreenThread(object):
28 """Wrapper for GreenThread objects to provide Thread-like attributes
31 def __init__(self, g):
34 self._name = 'GreenThread-%d' % _count
38 return '<_GreenThread(%s, %r)>' % (self._name, self._g)
40 def join(self, timeout=None):
47 def setName(self, name):
48 self._name = str(name)
51 name = property(getName, setName)
53 ident = property(lambda self: id(self._g))
59 daemon = property(lambda self: True)
70 # Some third-party packages (lockfile) will try to patch the
71 # threading.Thread class with a get_name attribute if it doesn't
72 # exist. Since we might return Thread objects from the original
73 # threading package that won't get patched, let's make sure each
74 # individual object gets patched too our patched threading.Thread
75 # class has been patched. This is why monkey patching can be bad...
78 __threading = __import__('threading')
80 if (hasattr(__threading.Thread, 'get_name') and
81 not hasattr(t, 'get_name')):
82 t.get_name = t.getName
87 g = greenlet.getcurrent()
89 # Not currently in a greenthread, fall back to standard function
90 return _fixup_thread(__orig_threading.current_thread())
93 active = __threadlocal.active
94 except AttributeError:
95 active = __threadlocal.active = {}
100 # Add green thread to active if we can clean it up on exit
105 except AttributeError:
106 # Not a GreenThread type, so there's no way to hook into
107 # the green thread exiting. Fall back to the standard
109 t = _fixup_thread(__orig_threading.currentThread())
111 t = active[id(g)] = _GreenThread(g)
115 currentThread = current_thread