コード例 #1
0
 def run(self):
     from random import random
     counter = 0
     while counter < self.quota:
         counter = counter + 1
         self.queue.put("%s.%d" % (self.getName(), counter))
         _sleep(random() * 0.00001)
コード例 #2
0
 def run(self):
     from random import random
     counter = 0
     while counter < self.quota:
         counter = counter + 1
         self.queue.put("%s.%d" % (self.getName(), counter))
         _sleep(random() * 0.00001)
コード例 #3
0
 def start(self):
     if not self.__initialized:
         raise RuntimeError("thread.__init__() not called")
     if self.__started:
         raise RuntimeError("thread already started")
     if __debug__:
         self._note("%s.start(): starting thread", self)
     _active_limbo_lock.acquire()
     _limbo[self] = self
     _active_limbo_lock.release()
     _start_new_thread(self.__bootstrap, ())
     self.__started = True
     _sleep(0.000001)    # 1 usec, to let the thread run (Solaris hack)
コード例 #4
0
 def start(self):
     if not self.__initialized:
         raise RuntimeError("thread.__init__() not called")
     if self.__started:
         raise RuntimeError("thread already started")
     if __debug__:
         self._note("%s.start(): starting thread", self)
     _active_limbo_lock.acquire()
     _limbo[self] = self
     _active_limbo_lock.release()
     _start_new_thread(self.__bootstrap, ())
     self.__started = True
     _sleep(0.000001)  # 1 usec, to let the thread run (Solaris hack)
コード例 #5
0
 def wait(self, timeout=None):
     if not self._is_owned():
         raise RuntimeError("cannot wait on un-aquired lock")
     waiter = _allocate_lock()
     waiter.acquire()
     self.__waiters.append(waiter)
     saved_state = self._release_save()
     try:    # restore state no matter what (e.g., KeyboardInterrupt)
         if timeout is None:
             waiter.acquire()
             if __debug__:
                 self._note("%s.wait(): got it", self)
         else:
             # Balancing act:  We can't afford a pure busy loop, so we
             # have to sleep; but if we sleep the whole timeout time,
             # we'll be unresponsive.  The scheme here sleeps very
             # little at first, longer as time goes on, but never longer
             # than 20 times per second (or the timeout time remaining).
             endtime = _time() + timeout
             delay = 0.0005 # 500 us -> initial delay of 1 ms
             while True:
                 gotit = waiter.acquire(0)
                 if gotit:
                     break
                 remaining = endtime - _time()
                 if remaining <= 0:
                     break
                 delay = min(delay * 2, remaining, .05)
                 _sleep(delay)
             if not gotit:
                 if __debug__:
                     self._note("%s.wait(%s): timed out", self, timeout)
                 try:
                     self.__waiters.remove(waiter)
                 except ValueError:
                     pass
             else:
                 if __debug__:
                     self._note("%s.wait(%s): got it", self, timeout)
     finally:
         self._acquire_restore(saved_state)
コード例 #6
0
 def wait(self, timeout=None):
     if not self._is_owned():
         raise RuntimeError("cannot wait on un-aquired lock")
     waiter = _allocate_lock()
     waiter.acquire()
     self.__waiters.append(waiter)
     saved_state = self._release_save()
     try:  # restore state no matter what (e.g., KeyboardInterrupt)
         if timeout is None:
             waiter.acquire()
             if __debug__:
                 self._note("%s.wait(): got it", self)
         else:
             # Balancing act:  We can't afford a pure busy loop, so we
             # have to sleep; but if we sleep the whole timeout time,
             # we'll be unresponsive.  The scheme here sleeps very
             # little at first, longer as time goes on, but never longer
             # than 20 times per second (or the timeout time remaining).
             endtime = _time() + timeout
             delay = 0.0005  # 500 us -> initial delay of 1 ms
             while True:
                 gotit = waiter.acquire(0)
                 if gotit:
                     break
                 remaining = endtime - _time()
                 if remaining <= 0:
                     break
                 delay = min(delay * 2, remaining, .05)
                 _sleep(delay)
             if not gotit:
                 if __debug__:
                     self._note("%s.wait(%s): timed out", self, timeout)
                 try:
                     self.__waiters.remove(waiter)
                 except ValueError:
                     pass
             else:
                 if __debug__:
                     self._note("%s.wait(%s): got it", self, timeout)
     finally:
         self._acquire_restore(saved_state)
コード例 #7
0
def _test():

    class BoundedQueue(_Verbose):

        def __init__(self, limit):
            _Verbose.__init__(self)
            self.mon = RLock()
            self.rc = Condition(self.mon)
            self.wc = Condition(self.mon)
            self.limit = limit
            self.queue = deque()

        def put(self, item):
            self.mon.acquire()
            while len(self.queue) >= self.limit:
                self._note("put(%s): queue full", item)
                self.wc.wait()
            self.queue.append(item)
            self._note("put(%s): appended, length now %d",
                       item, len(self.queue))
            self.rc.notify()
            self.mon.release()

        def get(self):
            self.mon.acquire()
            while not self.queue:
                self._note("get(): queue empty")
                self.rc.wait()
            item = self.queue.popleft()
            self._note("get(): got %s, %d left", item, len(self.queue))
            self.wc.notify()
            self.mon.release()
            return item

    class ProducerThread(Thread):

        def __init__(self, queue, quota):
            Thread.__init__(self, name="Producer")
            self.queue = queue
            self.quota = quota

        def run(self):
            from random import random
            counter = 0
            while counter < self.quota:
                counter = counter + 1
                self.queue.put("%s.%d" % (self.getName(), counter))
                _sleep(random() * 0.00001)


    class ConsumerThread(Thread):

        def __init__(self, queue, count):
            Thread.__init__(self, name="Consumer")
            self.queue = queue
            self.count = count

        def run(self):
            while self.count > 0:
                item = self.queue.get()
                print item
                self.count = self.count - 1

    NP = 3
    QL = 4
    NI = 5

    Q = BoundedQueue(QL)
    P = []
    for i in range(NP):
        t = ProducerThread(Q, NI)
        t.setName("Producer-%d" % (i+1))
        P.append(t)
    C = ConsumerThread(Q, NI*NP)
    for t in P:
        t.start()
        _sleep(0.000001)
    C.start()
    for t in P:
        t.join()
    C.join()
コード例 #8
0
def _test():
    class BoundedQueue(_Verbose):
        def __init__(self, limit):
            _Verbose.__init__(self)
            self.mon = RLock()
            self.rc = Condition(self.mon)
            self.wc = Condition(self.mon)
            self.limit = limit
            self.queue = deque()

        def put(self, item):
            self.mon.acquire()
            while len(self.queue) >= self.limit:
                self._note("put(%s): queue full", item)
                self.wc.wait()
            self.queue.append(item)
            self._note("put(%s): appended, length now %d", item,
                       len(self.queue))
            self.rc.notify()
            self.mon.release()

        def get(self):
            self.mon.acquire()
            while not self.queue:
                self._note("get(): queue empty")
                self.rc.wait()
            item = self.queue.popleft()
            self._note("get(): got %s, %d left", item, len(self.queue))
            self.wc.notify()
            self.mon.release()
            return item

    class ProducerThread(Thread):
        def __init__(self, queue, quota):
            Thread.__init__(self, name="Producer")
            self.queue = queue
            self.quota = quota

        def run(self):
            from random import random
            counter = 0
            while counter < self.quota:
                counter = counter + 1
                self.queue.put("%s.%d" % (self.getName(), counter))
                _sleep(random() * 0.00001)

    class ConsumerThread(Thread):
        def __init__(self, queue, count):
            Thread.__init__(self, name="Consumer")
            self.queue = queue
            self.count = count

        def run(self):
            while self.count > 0:
                item = self.queue.get()
                print item
                self.count = self.count - 1

    NP = 3
    QL = 4
    NI = 5

    Q = BoundedQueue(QL)
    P = []
    for i in range(NP):
        t = ProducerThread(Q, NI)
        t.setName("Producer-%d" % (i + 1))
        P.append(t)
    C = ConsumerThread(Q, NI * NP)
    for t in P:
        t.start()
        _sleep(0.000001)
    C.start()
    for t in P:
        t.join()
    C.join()