def enqueue(self, *args, **kwargs): event = self._policy.getEvent(*args, **kwargs) if self._policy.argumentPassingMode == eventpy.policy.argumentPassingExcludeEvent: args = args[1:] queuedEvent = QueuedEvent(event, args, kwargs) with lockguard.LockGuard(self._queueListLock): self._queueList.append(queuedEvent) if self._doCanProcess(): with lockguard.LockGuard(self._queueListMutex): self._queueListConditionVariable.notify()
def forEach(self, func): with lockguard.LockGuard(self._lock): node = self._list.getHead() counter = self._currentCounter invoke = _selectForEachInvoke(func) while node is not None: nodeCounter = node.getData()._counter if nodeCounter != 0 and counter >= nodeCounter: invoke(node) with lockguard.LockGuard(self._lock): node = node._next
def __call__(self, *args, **kwargs): with lockguard.LockGuard(self._lock): node = self._list.getHead() counter = self._currentCounter while node is not None: nodeCounter = node.getData()._counter if nodeCounter != 0 and counter >= nodeCounter: node.getData()._callback(*args, **kwargs) if not self._policy.canContinueInvoking(*args, **kwargs): break with lockguard.LockGuard(self._lock): node = node._next
def takeEvent(self): if self._queueList: with lockguard.LockGuard(self._queueListLock): if self._queueList: queuedEvent = self._queueList[0] self._queueList = self._queueList[1:] return queuedEvent return None
def waitFor(self, seconds): with lockguard.LockGuard(self._queueListMutex): startSeconds = time.time() while True: self._queueListConditionVariable.wait(0.001) if self._doCanProcess(): return True if time.time() - startSeconds >= seconds: return False
def process(self): if self._queueList: with lockguard.LockGuard(self._queueListLock): tempList = self._queueList self._queueList = [] if tempList: for queuedEvent in tempList: self.directDispatch(queuedEvent.event, *queuedEvent.args, **queuedEvent.kwargs) return True return False
def processOne(self): if self._queueList: queuedEvent = None with lockguard.LockGuard(self._queueListLock): if self._queueList: queuedEvent = self._queueList[0] self._queueList = self._queueList[1:] if queuedEvent is not None: self.directDispatch(queuedEvent.event, *queuedEvent.args, **queuedEvent.kwargs) return True return False
def processIf(self, func): if self._queueList: with lockguard.LockGuard(self._queueListLock): tempList = self._queueList self._queueList = [] if tempList: for queuedEvent in tempList: if func(*queuedEvent.args, **queuedEvent.kwargs): self.directDispatch(queuedEvent.event, *queuedEvent.args, **queuedEvent.kwargs) else: self._queueList.append(queuedEvent) return True return False
def _getNextCounter(self): self._currentCounter += 1 result = self._currentCounter if result <= 0: # overflow, let's reset all nodes' counters. with lockguard.LockGuard(self._lock): node = self._list.getHead() while node is not None: node.getData()._counter = 1 node = node.getNext() if self._currentCounter < 0: self._currentCounter = 0 self._currentCounter += 1 result = self._currentCounter return result
def clearEvents(self): with lockguard.LockGuard(self._queueListLock): self._queueList = []
def __exit__(self, type, value, traceBack): self._queue._queueNotifyCounter -= 1 if self._queue._doCanNotifyQueueAvailable( ) and not self._queue.emptyQueue(): with lockguard.LockGuard(self._queue._queueListMutex): self._queue._queueListConditionVariable.notify()
def peekEvent(self): if self._queueList: with lockguard.LockGuard(self._queueListLock): if self._queueList: return self._queueList[0] return None
def wait(self): with lockguard.LockGuard(self._queueListMutex): while True: self._queueListConditionVariable.wait(0.001) if self._doCanProcess(): break
def _doFindCallableList(self, event): with lockguard.LockGuard(self._eventCallbackListMapLock): if event in self._eventCallbackListMap: return self._eventCallbackListMap[event] return None
def _getCallbackList(self, event): with lockguard.LockGuard(self._eventCallbackListMapLock): if event not in self._eventCallbackListMap: self._eventCallbackListMap[event] = callbacklist.CallbackList( self._policy) return self._eventCallbackListMap[event]