thread.py 945 B

1234567891011121314151617181920212223242526272829
  1. import threading
  2. class UserlessThread(Exception):
  3. def __str__(self):
  4. return "Current thread: %s is not an instance of InstanceThread \
  5. of InstanceInheritingThread" % str(threading.current_thread())
  6. class HasUser(object):
  7. def user(self): return self._user
  8. def assign_user(self): self._user = threading.current_thread().user()
  9. class InstanceThread(threading.Thread, HasUser):
  10. def __init__(self,user, *args, **kwargs):
  11. super(InstanceThread, self).__init__(*args, **kwargs)
  12. self._user = user
  13. class InstanceInheritingThread(threading.Thread, HasUser):
  14. def __init__(self, *args, **kwargs):
  15. self.assign_user()
  16. super(InstanceInheritingThread, self).__init__(*args, **kwargs)
  17. def user():
  18. try: return threading.current_thread().user()
  19. except AttributeError: raise UserlessThread()
  20. def apc(): return user().api_client
  21. def getsig(s): return user().signal(s)