def ChannelWait(chan, timeout = None): """Receive on a channel, but with a timeout. The optional timeout is specified in seconds. Returns (bool, result) with the bool being true if there was no timeout. """ if timeout is None: return (True, chan.receive()) waiting_tasklet = stackless.getcurrent() def break_wait(): try: blue.pyos.synchro.SleepWallclock(int(timeout * 1000)) except _TimeoutError: return with atomic(): if waiting_tasklet and waiting_tasklet.blocked: waiting_tasklet.raise_exception(_TimeoutError) with atomic(): breaker = new(break_wait) try: result = chan.receive() if breaker.blocked: breaker.raise_exception(_TimeoutError) return (True, result) except _TimeoutError: return (False, None) finally: waiting_tasklet = None
def ChannelWait(chan, timeout = None): if timeout is None: return (True, chan.receive()) waiting_tasklet = stackless.getcurrent() def break_wait(): try: blue.pyos.synchro.SleepWallclock(int(timeout * 1000)) except _TimeoutError: return with atomic(): if waiting_tasklet and waiting_tasklet.blocked: waiting_tasklet.raise_exception(_TimeoutError) with atomic(): breaker = new(break_wait) try: result = chan.receive() if breaker.blocked: breaker.raise_exception(_TimeoutError) return (True, result) except _TimeoutError: return (False, None) finally: waiting_tasklet = None
def ChannelWait(chan, timeout=None): if timeout is None: return (True, chan.receive()) waiting_tasklet = stackless.getcurrent() def break_wait(): try: blue.pyos.synchro.SleepWallclock(int(timeout * 1000)) except _TimeoutError: return with atomic(): if waiting_tasklet and waiting_tasklet.blocked: waiting_tasklet.raise_exception(_TimeoutError) with atomic(): breaker = new(break_wait) try: result = chan.receive() if breaker.blocked: breaker.raise_exception(_TimeoutError) return (True, result) except _TimeoutError: return (False, None) finally: waiting_tasklet = None
def __call__(self, *args, **kwds): with atomic(): self.tasklet = stackless.getcurrent() try: self.initiate_call(args, kwds) return self.wait() finally: self.channel.close() self.tasklet = None
def break_wait(): try: blue.pyos.synchro.SleepWallclock(int(timeout * 1000)) except _TimeoutError: return with atomic(): if waiting_tasklet and waiting_tasklet.blocked: waiting_tasklet.raise_exception(_TimeoutError)
def release(self, override = False): with atomic(): if self.strict and not override: if stackless.getcurrent() not in self.threads: raise RuntimeError, 'wrong tasklet releasing strict semaphore' self.count += 1 self.threads.remove(stackless.getcurrent()) self.lockedWhen = None self._pump()
def try_acquire(self): with atomic(): if self.strict: if self.count <= 0 and stackless.getcurrent() in self.threads: raise RuntimeError, 'tasklet deadlock, acquiring tasklet holds strict semaphore' if self.count > 0: self.count -= 1 self.lockedWhen = blue.os.GetWallclockTime() self.threads.append(stackless.getcurrent()) return True return False
def acquire(self): with atomic(): if self.strict: if self.count <= 0 and stackless.getcurrent() in self.threads: raise RuntimeError, 'tasklet deadlock, acquiring tasklet holds strict semaphore' while self.count == 0: self.n_waiting += 1 try: self.waiting.receive() except: self._safe_pump() raise finally: self.n_waiting -= 1 self.count -= 1 self.lockedWhen = blue.os.GetWallclockTime() self.threads.append(stackless.getcurrent())
def cancel(self, *args): """rase the CancelledError on any waiting tasklet""" with atomic(): if self.tasklet: self.tasklet.throw(CancelledError, args)