added user level method to get current user of thread

This commit is contained in:
Rudi Grinberg 2012-11-07 22:53:39 -05:00
parent 7e5ec6505b
commit ed00089a1a
1 changed files with 9 additions and 3 deletions

View File

@ -1,10 +1,12 @@
import threading
tc = threading.local()
class UserlessThread(Exception):
def __str__():
return "Current thread: %s is not an instance of InstanceThread \
of InstanceInheritingThread" % str(threading.current_thread())
class HasUser(object):
def user(self):
return self._user
def user(self): return self._user
class InstanceThread(threading.Thread, HasUser):
def __init__(self,user, *args, **kwargs):
@ -15,3 +17,7 @@ class InstanceInheritingThread(threading.Thread, HasUser):
def __init__(self, *args, **kwargs):
self._user = threading.current_thread().user()
super(InstanceInheritingThread, self).__init__(*args, **kwargs)
def user():
try: return threading.current_thread().user()
except AttributeError: raise UserlessThread()