def Retrieve( self, evt, *args, **kw ): # 回收线程事件等待线程处理 # print( '%X -> %X Retrieve: %d' % (get_ident(), self.__thread.ident, evt) ) def __PostEvent( event, *args, **kw ): self.__queue.put( (event, args, kw) ) self.__event.set() ThreadSafe.SafeAccess( self.__lock, lambda : __PostEvent( evt, *args, **kw ) )
def PostEvent(self, event, *args, **kw): # print( '%X -> %X PostEvent: %d' % (get_ident(), self.__thread.ident, event) ) def __PostEvent(event, *args, **kw): self.__queue.put((event, args, kw)) self.__event.set() ThreadSafe.SafeAccess(self.__lock, lambda: __PostEvent(event, *args, **kw))
def SafeStopAllWork( self ): def StopAllWork( stations ): # print( 'Stop All' ) for station in stations: evt, lock, tasks, tdic = station for tid, worker in tdic.items(): worker.stop() ThreadSafe.SafeAccess( self._taskLock, lambda : StopAllWork( self.__stations ) )
def __GetTask( self ): def pop(): task = None if len(self.__taskQueue) > 0: task = self.__taskQueue.popleft() else: self.__event.clear() return task return ThreadSafe.SafeAccess( self.__locker, pop )
def _WaitForEvt( self, event ): # 陷入 Wait 循环事件处理 self.__wait = True self.RegEvt( event, self.__UntilEvt ) while self.__wait: if self.__event.is_set(): try: evt = ThreadSafe.SafeAccess( self.__lock, lambda : self.__queue.get( block=False, timeout=None ) ) # evt = self.__queue.get( block = False, timeout = 0.01 ) self.__Dispatch( evt ) ThreadSafe.SafeAccess( self.__lock, lambda : self.__ClearEvent() ) # if self.__queue.empty(): # self.__event.clear() except Empty: time.sleep(0.01) # pass else: time.sleep(0.01) # pass self.UnRegEvt( event, self.__UntilEvt )
def SubmitTask( self, task ): ''' 添加任务 Params: task 任务 ''' if task.phase < self.__phase: evt, lock, tasks, tids = self.__stations[task.phase] ThreadSafe.SafeAccess( lock, lambda : opList( tasks, task ) ) evt.set() else: LOG_ERROR('任务超出流水线')
def __Run(self): def ClearEvent(): if self.__queue.empty(): self.__event.clear() while self.__active: if self.__event.is_set(): # pass try: event = ThreadSafe.SafeAccess( self.__lock, lambda: self.__queue.get(block=False, timeout=None)) #if self.__lock.acquire(): # event = self.__queue.get( block = False, timeout = 0.001 ) # self.__lock.release() self.__Dispatch(event) ThreadSafe.SafeAccess(self.__lock, ClearEvent) # if self.__queue.empty(): # self.__event.clear() except Empty: time.sleep(0) # pass else: time.sleep(0)
def _Loop( self ): ''' 进入事件循环 ''' while self.__active: # print( '0', end='' ) if self.__event.is_set(): try: # print( self.__active and '1' or '0', end='' ) event = ThreadSafe.SafeAccess( self.__lock, lambda : self.__GetEvent() ) # event = self.__queue.get( block = False, timeout = None ) # event = self.__queue.get_nowait() self.__Dispatch( event ) # print( '2', end='' ) ThreadSafe.SafeAccess( self.__lock, lambda : self.__ClearEvent() ) # if self.__queue.empty(): # self.__event.clear() # print( '3', end='' ) except Empty: time.sleep(0.01) # pass else: # print( '.', end='' ) time.sleep(0.01)