def iter_context_objects(cls): """Returns an iterator over all objects for the combined application and context cache. """ objects = cls._co_cache.get(current_thread()) if objects is None: if len(cls._co_cache) > _MAX_CONTEXT_OBJECT_CACHE: cls._co_cache.clear() objects = cls._co_global[:] objects.extend(getattr(cls._co_context, 'stack', ())) objects.sort(reverse=True) objects = [x[1] for x in objects] cls._co_cache[current_thread()] = objects return iter(objects)
def pop_thread(self): """Pops the context object from the stack.""" with self._co_context_lock: self._co_cache.pop(current_thread(), None) stack = getattr(self._co_context, 'stack', None) assert stack, 'no objects on stack' popped = stack.pop()[1] assert popped is self, 'popped unexpected object'
def push_thread(self, obj): with self._context_lock: self._cache.pop(current_thread(), None) item = (self._stackop(), obj) stack = getattr(self._context, 'stack', None) if stack is None: self._context.stack = [item] else: stack.append(item)
def pop_thread(self): self._context_lock.acquire() try: self._cache.pop(current_thread(), None) stack = getattr(self._context, 'stack', None) assert stack, 'no objects on stack' return stack.pop()[1] finally: self._context_lock.release()
def push_thread(self): """Pushes the context object to the thread stack.""" with self._co_context_lock: self._co_cache.pop(current_thread(), None) item = (self._co_stackop(), self) stack = getattr(self._co_context, 'stack', None) if stack is None: self._co_context.stack = [item] else: stack.append(item)
def push_thread(self, obj): self._context_lock.acquire() try: self._cache.pop(current_thread(), None) item = (self._stackop(), obj) stack = getattr(self._context, 'stack', None) if stack is None: self._context.stack = [item] else: stack.append(item) finally: self._context_lock.release()
def iter_context_objects(self): """Returns an iterator over all objects for the combined application and context cache. """ tid = current_thread() objects = self._cache.get(tid) if objects is None: if len(self._cache) > _MAX_CONTEXT_OBJECT_CACHE: self._cache.clear() objects = self._global[:] objects.extend(getattr(self._context, 'stack', ())) objects.sort(reverse=True) objects = [x[1] for x in objects] self._cache[tid] = objects return iter(objects)
def pop_thread(self): with self._context_lock: self._cache.pop(current_thread(), None) stack = getattr(self._context, 'stack', None) assert stack, 'no objects on stack' return stack.pop()[1]
def wrapper(*args, **kwargs): assert current_thread() == _main_thread_id, msg return f(*args, **kwargs)
self.func() def main_thread(func, *args, **kwargs): """ Schedue `func` to be called on the main thread. """ pool = NSAutoreleasePool.new() obj = MainThreadHelper.new() obj.func = lambda: func(*args, **kwargs) selector = objc.selector(obj.onMainThread, signature='v@:') later = obj.performSelectorOnMainThread_withObject_waitUntilDone_ later(selector, None, False) _main_thread_id = current_thread() def assert_main_thread(f): """ Make sure the wrapped function is called on the main thread. """ msg = "%r must be run on main thread" % f @functools.wraps(f) def wrapper(*args, **kwargs): assert current_thread() == _main_thread_id, msg return f(*args, **kwargs) return wrapper def connect_to_server(reg, config): run_loop = get_imap_loop(config)