예제 #1
0
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
예제 #2
0
파일: uthread.py 프로젝트: Pluckyduck/eve
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
예제 #3
0
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
예제 #4
0
 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
예제 #5
0
    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)
예제 #6
0
 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()
예제 #7
0
파일: uthread.py 프로젝트: Pluckyduck/eve
    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)
예제 #8
0
파일: uthread.py 프로젝트: Pluckyduck/eve
 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()
예제 #9
0
 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
예제 #10
0
파일: uthread.py 프로젝트: Pluckyduck/eve
 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
예제 #11
0
    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())
예제 #12
0
파일: uthread.py 프로젝트: Pluckyduck/eve
    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())
예제 #13
0
 def cancel(self, *args):
     """rase the CancelledError on any waiting tasklet"""
     with atomic():
         if self.tasklet:
             self.tasklet.throw(CancelledError, args)