added user level method to get current user of thread
This commit is contained in:
parent
7e5ec6505b
commit
ed00089a1a
|
@ -1,10 +1,12 @@
|
||||||
import threading
|
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):
|
class HasUser(object):
|
||||||
def user(self):
|
def user(self): return self._user
|
||||||
return self._user
|
|
||||||
|
|
||||||
class InstanceThread(threading.Thread, HasUser):
|
class InstanceThread(threading.Thread, HasUser):
|
||||||
def __init__(self,user, *args, **kwargs):
|
def __init__(self,user, *args, **kwargs):
|
||||||
|
@ -15,3 +17,7 @@ class InstanceInheritingThread(threading.Thread, HasUser):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self._user = threading.current_thread().user()
|
self._user = threading.current_thread().user()
|
||||||
super(InstanceInheritingThread, self).__init__(*args, **kwargs)
|
super(InstanceInheritingThread, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def user():
|
||||||
|
try: return threading.current_thread().user()
|
||||||
|
except AttributeError: raise UserlessThread()
|
||||||
|
|
Loading…
Reference in New Issue