Пример #1
0
class SimpleBlockingQueue(object):
    def __init__(self):
        self.items = deque()
        self.condition = Condition()
        self.interrupted = False

    def put(self, item):
        with self.condition:
            self.items.pushleft(item)

    def take(self, block=False):
        with self.condition:
            if block:
                while not self.items:
                    self.condition.wait()
                    if self.interrupted:
                        self.interrupted = False
                        return None
            elif not self.items:
                return None

            return self.items.popright()

    def interrupt(self):
        with self.condition:
            self.interrupted = True
            self.condition.notifyAll()
Пример #2
0
class NextCritical(object):
    def __init__(self, N):
        self.condition = Condition()
        self.N = N
        self.calls = [0] * N
        self.max_calls = 1

    def is_ok(self, i):
        with self.condition:
            # print '%r / %d' % (self.calls, self.max_calls)
            return self.calls[i] < self.max_calls

    def wait(self, i):
        with self.condition:
            while not self.is_ok(i):
                self.condition.wait()

    def inc(self, i):
        with self.condition:
            if self.is_ok(i):
                self.calls[i] += 1
                if sum(self.calls) == self.max_calls * self.N:
                    self.max_calls += 1
                    self.condition.notifyAll()
            else:
                raise RuntimeError('Cannot inc while condition is not met.')
Пример #3
0
class KeyCache:
    def __init__(self, capacity):
        self.buffer_capacity = capacity
        self.buffer_size = 0
        self.cv = Condition()
        self.q = []
        self.producer_block_size = BLOCK_SIZE


    def getKey(self):

        self.cv.acquire()
        while self.buffer_size == 0:
            self.cv.wait()

        item = self.q.pop(0)
        self.buffer_size -= 1

        self.cv.notifyAll()
        self.cv.release()


        return item

    # insert multiple string equal to 
    def putKeys(self, items):

        self.cv.acquire()
        while self.buffer_size >= self.buffer_capacity - self.producer_block_size:
            self.cv.wait()

        self.q += items
        self.buffer_size += len(items)
        self.cv.notifyAll()
        self.cv.release()
Пример #4
0
class Heartbeater(object):
    interval = 5

    def __init__(self, api, jobstep_id, interval=None):
        self.api = api
        self.jobstep_id = jobstep_id
        self.cv = Condition()
        self.finished = Event()

        if interval is not None:
            self.interval = interval

    def wait(self):
        with self.cv:
            self.finished.clear()
            while not self.finished.is_set():
                data = self.api.get_jobstep(self.jobstep_id)
                if data['status']['id'] == 'finished':
                    self.finished.set()
                    break

                self.cv.wait(self.interval)

    def close(self):
        with self.cv:
            self.finished.set()
            self.cv.notifyAll()
Пример #5
0
class NiceTQueue():
    def __init__(self):
        self._list = []
        self.condition = Condition()

    def insert(self, index, item):
        with self.condition:
            self._list.insert(index, item)
            self.condition.notifyAll()

    def put(self, item):
        with self.condition:
            self._list.append(item)
            self.condition.notifyAll()

    def pop(self, index, timeout=None):
        if not len(self._list):
            with self.condition:
                self.condition.wait(timeout)
        if len(self._list) < index + 1:
            raise queue.Empty()
        return self._list.pop(index)

    def get(self, timeout=None):
        return self.pop(0, timeout)

    def qsize(self):
        return len(self._list)

    def clear(self):
        self._list.clear()
Пример #6
0
class PostBox(list):

    def __init__(self, *a):
        list.__init__(self, *a)
        self.lever = Condition()
        self.open = True
        self.done = False

    def wait(self):
        self.lever.acquire()
        if not self.done:
            self.lever.wait()
        self.lever.release()
        return self.result

    def wakeup(self, data):
        self.result = data
        self.lever.acquire()
        self.done = True
        self.lever.notifyAll()
        self.lever.release()

    def append(self, e):
        self.lever.acquire()
        if not self.open:
            raise BoxClosedErr
        list.append(self, e)
        self.lever.release()

    def close(self):
        self.lever.acquire()
        self.open = False
        self.lever.release()
Пример #7
0
class DataQueue(Queue):
    def __init__(self, count):
        Queue.__init__(self)
        self._count = count
        self._cond = Condition()

    def get(self, block=True, timeout=None):
        self._cond.acquire()
        if self.empty():
            if not self._count:
                raise Empty()
            elif block:
                self._cond.wait(timeout)
                if self.empty() and not self._count:
                    self._cond.release()
                    raise Empty()
        self._cond.release()
        return Queue.get(self, block, timeout)

    def put(self, data):
        self._cond.acquire()
        self._count = self._count - 1
        Queue.put(self, data)
        if self._count:
            self._cond.notify()
        else:
            self._cond.notifyAll()
        self._cond.release()

    def remain(self):
        return self._count + self.unfinished_tasks
Пример #8
0
class ConnectionPool:
    def __init__(self, creator, max_active, *args, **kwargs):
        self.max_active = max_active
        self.queue = Queue(max_active)
        self._lock = Condition()
        self._connections = 0
        self._creator = creator.connect
        self._creator_constructor_args = (args, kwargs)

    def get_connection(self):
        self._lock.acquire()
        try:
            if self._connections < self.max_active:
                conn = Connection(
                    self._creator(*self._creator_constructor_args[0],
                                  **self._creator_constructor_args[1]), self)
                self.queue.put(conn)
                self._connections += 1
            if self.queue.empty():
                self._lock.wait()
            return self.queue.get()
        finally:
            self._lock.release()

    def release(self, conn):
        self._lock.acquire()
        try:
            self.queue.put(conn)
            self._lock.notifyAll()
        finally:
            self._lock.release()

    def __del__(self):
        while not self.queue.empty():
            self.queue.get().close()
Пример #9
0
class File:
    def __init__(self):
        self.liste = []
        self.cond = Condition()
        self.premier = 0
        self.dernier = 0

    def affichage(self):
        ch = "File = <"
        for e in self.liste:
            ch += str(e)
            ch += ", "
        ch += ">"
        text0.config(text=ch)

    def ajoute(self):
        with self.cond:
            while (len(self.liste) == 20):
                self.cond.wait()
            self.dernier = random.randint(1, 100)
            self.liste.append(self.dernier)
            self.cond.notifyAll()

    def retrait(self):
        with self.cond:
            while (len(self.liste) == 0):
                self.cond.wait()
            self.premier = self.liste[0]
            del self.liste[0]
            self.cond.notifyAll()
class ReadWriteLock:

    def __init__(self):
        self.read_ready = Condition(Lock())
        self.readers = 0

    def acquire_read(self):
        self.read_ready.acquire()
        try:
            self.readers += 1
        finally:
            self.read_ready.release()

    def release_read(self):
        self.read_ready.acquire()
        try:
            self.readers -= 1
            if not self.readers:
                self.read_ready.notifyAll()
        finally:
            self.read_ready.release()

    def acquire_write(self):
        self.read_ready.acquire()
        while self.readers > 0:
            self.read_ready.wait()

    def release_write(self):
        self.read_ready.release()
Пример #11
0
class ReadWriteLock():
    def __init__(self):
        self._readReady = Condition()
        self._readers = 0
        self._writes = 0

    def acquireRead(self):
        while self._writes > 0:
            self._readReady.wait()
        self._readReady.acquire()
        try:
            self._readers += 1
        finally:
            self._readReady.release()

    def releaseRead(self):
        self._readReady.acquire()
        try:
            self._readers -= 1
            if not self._readers:
                self._readReady.notifyAll()
        finally:
            self._readReady.release()

    def acquireWrite(self):
        self._writes += 1
        self._readReady.acquire()
        while self._readers > 0:
            self._readReady.wait()

    def releaseWrite(self):
        self._writes -= 1
        self._readReady.release()
Пример #12
0
class Observable:

    def __init__(self):
        self.mutex = RLock()
        self.c = Condition(self.mutex)
        self.observers = []

    def register(self, observer):
        with self.mutex:
            if observer not in self.observers:
                self.observers.append(observer)

    def unregister(self, observer):
        with self.mutex:
            if observer in self.observers:
                self.observers.remove(observer)

    def unregister_all(self):
        with self.mutex:
            if self.observers:
                del self.observers[:]

    def update_observers(self, *args, **kwargs):
        with self.mutex:
            for observer in self.observers:
                self.c.notifyAll()
                observer.update(*args, **kwargs)
Пример #13
0
class File():
	def __init__(self):
		self.file = []
		self.Cond = Condition()

	def deposer(self,prod):
		with self.Cond :
			b = randint(0,100)
			prod.label["text"] = "Producteur : Ajout de l'entier " + str(b) + " dans la file"
			while(len(self.file)>20):
				prod.label["text"] = "Producteur : File pleine. En attente d'une place pour l'entier : " + str(b) 
				self.Cond.wait()
			self.file.append(b)
			label["text"] = "File : " + str(self.file)
			self.Cond.notifyAll()

	def retirer(self,consom):
		with self.Cond :
			while(len(self.file)==0):
				consom.label["text"] = "Consommateurs "+ str(consom.n)+" : File vide. En attente de l'ajout d'un entier"
				self.Cond.wait()
			consom.label["text"] = "Consommateurs "+str(consom.n)+" : Retrait de l'entier " + str(self.file[0]) + " dans la file"
			r = self.file[0]
			self.file.remove(self.file[0])
			consom.label["text"] = "Consommateurs "+str(consom.n)+" : Retrait de " + str(r) + " effectué"
			label["text"] = "File : " + str(self.file)
			self.Cond.notifyAll()
Пример #14
0
class locked_deque:
    def __init__(self):
        self._queue = deque()
        self.cv = Condition()
        return

    def isempty(self):
        return not bool(self._queue)

    def pop(self):
        with self.cv:
            if (self.isempty()):
                self.cv.wait()
            if (self.isempty()):
                return None
            return self._queue.popleft()

    def append(self, item):
        with self.cv:
            self._queue.append(item)
            self.cv.notifyAll()

    def wait(self, t):
        with self.cv:
            self.cv.wait(t)
Пример #15
0
class File():
    def __init__(self):
        self.file = []
        self.Cond = Condition()

    def deposer(self, prod):
        with self.Cond:
            b = randint(0, 100)
            prod.label["text"] = "Producteur : ajout de l'entier " + str(b)
            while (len(self.file) > 20):
                prod.label["text"] = "Producteur : file pleine."
                self.Cond.wait()
            self.file.append(b)
            fileLabel["text"] = "File : " + str(self.file)
            self.Cond.notifyAll()

    def retirer(self, consom):
        with self.Cond:
            while (len(self.file) == 0):
                consom.label["text"] = "Consommateur " + str(
                    consom.n) + " : file vide."
                self.Cond.wait()
            r = self.file[0]
            self.file.remove(self.file[0])
            consom.label["text"] = "Consommateurs " + str(
                consom.n) + " : retrait de " + str(r) + " effectué"
            fileLabel["text"] = "File : " + str(self.file)
            self.Cond.notifyAll()
Пример #16
0
class CountDownLatch:
    def __init__(self, count):
        self.count = count
        self.condition = Condition()

    def await_(self):
        try:
            self.condition.acquire()
            while self.count > 0:
                self.condition.wait()
        finally:
            self.condition.release()

    def await_after(self, timeout):
        try:
            begin = time.time()
            while begin + timeout >= time.time() and self.count > 0:
                time.sleep(1)
        finally:
            pass

    def countDown(self):
        try:
            self.condition.acquire()
            self.count -= 1
            if self.count <= 0:
                self.condition.notifyAll()
        finally:
            self.condition.release()

    def getCount(self):
        return self.count
Пример #17
0
class MWPixelClock(object):
    def __init__(self, conduitName):
        self.conduit = Conduit(conduitName)
        self.conduit.initialize()
        self.conduit.register_local_event_code(0, '#stimDisplayUpdate')
        self.conduit.register_callback_for_name('#stimDisplayUpdate',
                                                self.receive_event)
        self.codes = []
        self.cond = Condition()
        self.maxCodes = 100

    def receive_event(self, event):
        for s in event.data:
            if s is None:
                continue
            if s.has_key('bit_code'):
                self.cond.acquire()
                self.codes.append((s['bit_code'], event.time / 1000000.))
                # if len(self.codes) > 2:
                #     #logging.debug('MW bit_code = %i' % s['bit_code'])
                #     #print s['bit_code']
                #     #logging.debug("MW Delta: %s" % delta_code(self.codes[-1][0], self.codes[-2][0]))
                while len(self.codes) > self.maxCodes:
                    self.codes.pop(0)
                self.cond.notifyAll()
                self.cond.release()
Пример #18
0
class WebcamVideoStream:
    def __init__(self, logger, src=0):
        self.stream = cv2.VideoCapture(src)
        (self.grabbed, self.frame) = self.stream.read()
        self.stopped = False
        self.should_capture_frame = Condition()
        self.logger = logger
        self.n = 0

    def start(self):
        Thread(target=self.update, args=()).start()
        return self

    def update(self):
        while not self.stopped:

            self.n += 1

            logger.debug('taking a shot! %s' % self.n)
            self.frame = cv2.cvtColor(self.stream.read()[1], cv2.COLOR_BGR2RGB)
            logger.debug('took a shot! %s' % self.n)
            with self.should_capture_frame:
                self.should_capture_frame.wait()

    def read(self):
        to_return = numpy.copy(self.frame)
        with self.should_capture_frame:
            self.should_capture_frame.notifyAll()
        logger.debug('returning a frame %s' % self.n)

        return to_return

    def stop(self):
        self.stopped = True
Пример #19
0
class Semaphore(object):
    def __init__(self, count):
        self.count = count
        self.left = count
        self.cond = Condition()

    def acquire(self):
        self.cond.acquire()

        while self.left == 0:
            self.cond.wait()

        self.left -= 1

        self.cond.notifyAll()
        self.cond.release()

    def release(self):
        self.cond.acquire()

        while self.left == self.count:
            self.cond.wait()

        self.left += 1

        self.cond.notifyAll()
        self.cond.release()
Пример #20
0
class PeriodicWorker(Thread):

    def __init__(self, func, config, duration=600, logger=cherrypy.log, logDB=None):
        # use default RLock from condition
        # Lock wan't be shared between the instance used  only for wait
        # func : function or callable object pointer
        self.wakeUp = Condition()
        self.stopFlag = False
        self.taskFunc = func
        self.config = config
        self.duration = duration
        self.logger = logger
        self.logDB = logDB

        try:
            name = func.__name__
            print(name)
        except AttributeError:
            name = func.__class__.__name__
            print(name)

        Thread.__init__(self, name=name)
        cherrypy.engine.subscribe('start', self.start, priority=100)
        cherrypy.engine.subscribe('stop', self.stop, priority=100)

    def stop(self):
        self.wakeUp.acquire()
        self.stopFlag = True
        self.wakeUp.notifyAll()
        self.wakeUp.release()

    def run(self):

        while not self.stopFlag:
            self.wakeUp.acquire()
            try:
                self.taskFunc(self.config)
                self.heartBeatInfoToLogDB()
            except Exception as e:
                self.logger.error("Periodic Thread ERROR %s.%s %s"
                % (getattr(e, "__module__", "__builtins__"),
                e.__class__.__name__, str(e)))
                traceMsg = traceback.format_exc()
                for line in traceMsg.rstrip().split("\n"):
                    self.logger.error(" " + line)
                self.heartBeatErrorToLogDB(traceMsg)

            self.wakeUp.wait(self.duration)
            self.wakeUp.release()

    def heartBeatInfoToLogDB(self):
        if self.logDB:
            self.logDB.delete(mtype="error", this_thread=True)
            self.logDB.post(mtype="info")
        return

    def heartBeatErrorToLogDB(self, msg):
        if self.logDB:
            self.logDB.post(msg=msg, mtype="error")
        return
class CountDownLatch:
    """
    CountDownLatch can be used for async testing
    """
    def __init__(self, count=1):
        self.count = count
        self.condition = Condition()

    def awaits(self, timeout=None, to_count=0):
        try:
            self.condition.acquire()
            start = datetime.now()
            while self.count > to_count:
                self.condition.wait(timeout / 10)  # divides each step by 10
                if timeout is not None:
                    spent = (datetime.now() - start).seconds
                    if spent > timeout:
                        raise Exception(
                            "timeout after waiting for %d seconds!" % spent)
        finally:
            self.condition.release()

    def count_down(self):
        try:
            self.condition.acquire()
            self.count -= 1
            self.condition.notifyAll()
        finally:
            self.condition.release()

    def get_count(self):
        return self.count
Пример #22
0
class ReaderWriterLock:

  def __init__(self):
    self.condition_variable = Condition()
    self.writer_working = False
    self.readers = 0

  def acquire_reader_lock(self):
    self.condition_variable.acquire()
    while self.writer_working is True:
      self.condition_variable.wait()
    self.readers += 1
    self.condition_variable.release()

  def release_reader_lock(self):
    self.condition_variable.acquire()
    self.readers -= 1
    if self.readers == 0:
      self.condition_variable.notifyAll()
    self.condition_variable.release()

  def acquire_writer_lock(self):
    self.condition_variable.acquire()
    while self.readers != 0 or self.writer_working is True:
      self.condition_variable.wait()
    self.writer_working = True
    self.condition_variable.release()

  def release_writer_lock(self):
    self.condition_variable.acquire()
    self.writer_working = False
    self.condition_variable.notifyAll()
    self.condition_variable.release()
Пример #23
0
class AsyncChannel:
    # yes, i believe this is just Queue ... i was new to python and couldn't find it

    def __init__(self, buffer_size=1):
        self.buffer = []
        self.max_items = buffer_size
        self.lock = Lock()
        self.not_empty = Condition(self.lock)
        self.not_full = Condition(self.lock)

    def get(self):
        self.lock.acquire()
        while len(self.buffer) == 0:
            self.not_empty.wait()
        val = self.buffer.pop(0)
        self.not_full.notifyAll()
        self.lock.release()
        return val

    def put(self, val):
        self.lock.acquire()
        while len(self.buffer) == self.max_items:
            self.not_full.wait()
        self.buffer.append(val)
        self.not_empty.notifyAll()
        self.lock.release()
Пример #24
0
        class CountDownLatch(object):
            def __init__(self, count):
                self._count = count
                self._lock = Condition()

            def count_down(self):
                self._lock.acquire()
                self._count -= 1
                if self._count <= 0:
                    self._lock.notifyAll()
                self._lock.release()

            def wait(self, wait_secs=None):
                self._lock.acquire()
                if wait_secs is not None:
                    self._lock.wait(wait_secs)
                else:
                    while self._count > 0:
                        self._lock.wait()
                self._lock.release()

            def get_count(self):
                self._lock.acquire()
                count = self._count
                self._lock.release()
                return count
Пример #25
0
class Primitive:
    def __init__(self, gate=1, lock=None):
        self._Lock = lock if lock is not None else RLock()
        self._WakeUp = Condition(self._Lock)
        self._Gate = Semaphore(gate)

    def getLock(self):
        return self._Lock

    def __enter__(self):
        return self._Lock.__enter__()

    def __exit__(self, exc_type, exc_value, traceback):
        return self._Lock.__exit__(exc_type, exc_value, traceback)

    @synchronized
    def sleep(self, timeout=None, function=None, arguments=()):
        self._WakeUp.wait(timeout)
        if function is not None:
            return function(*arguments)

    # await is a reserved word in Python 3, use "wakeup" instead
    @synchronized
    def wakeup(self, n=1, all=False, function=None, arguments=()):
        if function is not None:
            function(*arguments)
        if all:
            self._WakeUp.notifyAll()
        else:
            self._WakeUp.notify(n)
Пример #26
0
class CountLatch(object):
    """Synchronization primitive with the capacity to count and latch.
    Counting up latches, while reaching zero when counting down un-latches.

    .. note::
        The CountLatch class has been included in Sardana
        on a provisional basis. Backwards incompatible changes
        (up to and including removal of the module) may occur if
        deemed necessary by the core developers.
    """
    def __init__(self):
        self.count = 0
        self.condition = Condition()

    def count_up(self):
        """Count up."""
        self.condition.acquire()
        self.count += 1
        self.condition.release()

    def count_down(self, _=None):
        """Count down."""
        self.condition.acquire()
        self.count -= 1
        if self.count <= 0:
            self.condition.notifyAll()
        self.condition.release()

    def wait(self):
        """Wait until the counter reaches zero."""
        self.condition.acquire()
        while self.count > 0:
            self.condition.wait()
        self.condition.release()
Пример #27
0
class IncomingMsgHandler(Runnable):
    def __init__(self, devs, output):
        self._queue = []
        self._lock = Condition()
        self._devs = devs
        self._output = output
        self._close = False

    def getMsg(self):
        with self._lock:
            while len(self._queue) == 0:
                self._lock.wait()
                if self._close: return None
            return self._queue.pop(0)

    def close(self):
        with self._lock:
            self._close = True
            self._lock.notifyAll()

    def addMsg(self, msg):
        with self._lock:
            self._queue.append(msg)
            self._lock.notifyAll()

    def run(self):
        while not self._close:
            msg = self.getMsg()
            if msg is not None:
                IncomingDataHandler(msg, self._output, self._devs).run()
Пример #28
0
class Barrier(object):
  def __init__(self, size):
    self.barrier_size = size
    self.reached_thread = 0
    self.released_thread = 0
    self.condition_variable = Condition()

  def reached(self):
    self.condition_variable.acquire()

    # block any new threads from proceeding until all the threads
    # have reached the previous Barrier having been released
    while self.reached_thread == self.barrier_size:
      self.condition_variable.wait()

    self.reached_thread += 1

    if self.reached_thread == self.barrier_size:
      self.released_thread = self.barrier_size
    else:
      while self.reached_thread < self.barrier_size:
        self.condition_variable.wait()

    self.released_thread -= 1

    if self.released_thread == 0:
      self.reached_thread = 0

    print("{0} released".format(current_thread().getName()))
    self.condition_variable.notifyAll()
    self.condition_variable.release()
Пример #29
0
    class _EventRef(object):
        """ Helper class which acts as a threading.Event, but which can be used
            to pass a reference when signaling the event.
        """
        def __init__(self):
            self._cond = Condition(Lock())
            self._flag = False
            self._ref = None

        def isSet(self):
            return self._flag

        def set(self, ref):
            with self._cond:
                assert self._ref is None
                self._ref = ref
                self._flag = True
                self._cond.notifyAll()

        def get(self, timeout=None):
            with self._cond:
                if not self._flag:
                    self._cond.wait(timeout)

                if not self._flag:
                    raise TimeoutExceeded('Could not get the reference.')

                return self._ref

        def clear(self, ref):
            with self._cond:
                self._ref = None
                self._flag = False
Пример #30
0
def test_lock2():
    cond = Condition()
    testlock = TestLock()

    def invork_init_pool():
        cond.acquire()
        try:
            cond.wait()
            testlock.init_pool()
        finally:
            cond.release()

    threads = []
    for i in range(100):
        t = Thread(target=invork_init_pool)
        t.start()
        threads.append(t)

    with cond:
        cond.notifyAll()

    for thread in threads:
        thread.join()

    print(testlock.get_pool())