Affinity() -> Affinity instance
| 24 | ################################################################################ |
| 25 | |
| 26 | class Affinity: |
| 27 | |
| 28 | "Affinity() -> Affinity instance" |
| 29 | |
| 30 | slots('thread, action') |
| 31 | |
| 32 | def __init__(self): |
| 33 | "Initializes instance with thread identity and job queue." |
| 34 | self.__thread = _thread.get_ident() |
| 35 | self.__action = queue.Queue() |
| 36 | |
| 37 | def __call__(self, func, *args, **kwargs): |
| 38 | "Executes function on creating thread and returns result." |
| 39 | if _thread.get_ident() == self.__thread: |
| 40 | while not self.__action.empty(): |
| 41 | self.__action.get_nowait()() |
| 42 | return func(*args, **kwargs) |
| 43 | delegate = _Delegate(func, args, kwargs) |
| 44 | self.__action.put_nowait(delegate) |
| 45 | return delegate.value |
| 46 | |
| 47 | ################################################################################ |
| 48 |