Python threading.Condition() Examples

The following are code examples for showing how to use threading.Condition(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pyblish-win   Author: pyblish   File: Queue.py    GNU Lesser General Public License v3.0 8 votes vote down vote up
def __init__(self, maxsize=0):
        self.maxsize = maxsize
        self._init(maxsize)
        # mutex must be held whenever the queue is mutating.  All methods
        # that acquire mutex must release it before returning.  mutex
        # is shared between the three conditions, so acquiring and
        # releasing the conditions also acquires and releases mutex.
        self.mutex = _threading.Lock()
        # Notify not_empty whenever an item is added to the queue; a
        # thread waiting to get is notified then.
        self.not_empty = _threading.Condition(self.mutex)
        # Notify not_full whenever an item is removed from the queue;
        # a thread waiting to put is notified then.
        self.not_full = _threading.Condition(self.mutex)
        # Notify all_tasks_done whenever the number of unfinished tasks
        # drops to zero; thread waiting to join() is notified to resume
        self.all_tasks_done = _threading.Condition(self.mutex)
        self.unfinished_tasks = 0 
Example 2
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: pubSub.py    MIT License 6 votes vote down vote up
def main():
  integers = []
  condition = threading.Condition()
  
  # Our Publisher
  pub1 = Publisher(integers, condition)
  pub1.start()

  # Our Subscribers
  sub1 = Subscriber(integers, condition)
  sub2 = Subscriber(integers, condition)
  sub1.start()
  sub2.start()

  ## Joining our Threads
  pub1.join()
  consumer1.join()
  consumer2.join() 
Example 3
Project: Cryptchat   Author: djohsson   File: gui.py    MIT License 6 votes vote down vote up
def __init__(self, parent, nick, networkhandler):
        tkinter.Tk.__init__(self, parent)
        self.grid()
        self.title("Cryptchat")
        self.out_message = []

        self.text = TextArea(self, height=25, width=50, font=("Droid Sans Mono", 12))
        self.text.grid(row=0, column=0, sticky="NW")
        self.text.pack()

        self.entry = TextEntry(self, nick, 50, font=("Droid Sans Mono", 12))
        self.entry.grid(row=2, column=0, sticky="SW")
        self.entry.pack()

        self.out_sem = Semaphore(0)
        self.out_condition = Condition()
        self.app_condition = Condition()

        sthread = SendThread(self, networkhandler)
        rthread = ReceiveThread(self, networkhandler)
        sthread.start()
        rthread.start()

        self.resizable(width=False, height=False)
        self.mainloop() 
Example 4
Project: openhatch   Author: campbe13   File: pool.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(self, cache, callback, accept_callback=None,
            timeout_callback=None, error_callback=None, soft_timeout=None,
            timeout=None):
        self._mutex = threading.Lock()
        self._cond = threading.Condition(threading.Lock())
        self._job = job_counter.next()
        self._cache = cache
        self._ready = False
        self._callback = callback
        self._accept_callback = accept_callback
        self._errback = error_callback
        self._timeout_callback = timeout_callback
        self._timeout = timeout
        self._soft_timeout = soft_timeout

        self._accepted = False
        self._worker_pid = None
        self._time_accepted = None
        cache[self._job] = self 
Example 5
Project: deb-python-cassandra-driver   Author: openstack   File: pool.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, host, host_distance, session):
        self.host = host
        self.host_distance = host_distance
        self._session = weakref.proxy(session)
        self._lock = Lock()
        # this is used in conjunction with the connection streams. Not using the connection lock because the connection can be replaced in the lifetime of the pool.
        self._stream_available_condition = Condition(self._lock)
        self._is_replacing = False

        if host_distance == HostDistance.IGNORED:
            log.debug("Not opening connection to ignored host %s", self.host)
            return
        elif host_distance == HostDistance.REMOTE and not session.cluster.connect_to_remote_hosts:
            log.debug("Not opening connection to remote host %s", self.host)
            return

        log.debug("Initializing connection for host %s", self.host)
        self._connection = session.cluster.connection_factory(host.address)
        self._keyspace = session.keyspace
        if self._keyspace:
            self._connection.set_keyspace_blocking(self._keyspace)
        log.debug("Finished initializing connection for host %s", self.host) 
Example 6
Project: deb-python-cassandra-driver   Author: openstack   File: pool.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, host, host_distance, session):
        self.host = host
        self.host_distance = host_distance

        self._session = weakref.proxy(session)
        self._lock = RLock()
        self._conn_available_condition = Condition()

        log.debug("Initializing new connection pool for host %s", self.host)
        core_conns = session.cluster.get_core_connections_per_host(host_distance)
        self._connections = [session.cluster.connection_factory(host.address)
                             for i in range(core_conns)]

        self._keyspace = session.keyspace
        if self._keyspace:
            for conn in self._connections:
                conn.set_keyspace_blocking(self._keyspace)

        self._trash = set()
        self._next_trash_allowed_at = time.time()
        self.open_count = core_conns
        log.debug("Finished initializing new connection pool for host %s", self.host) 
Example 7
Project: deb-python-cassandra-driver   Author: openstack   File: pool.py    Apache License 2.0 6 votes vote down vote up
def _wait_for_conn(self, timeout):
        start = time.time()
        remaining = timeout

        while remaining > 0:
            # wait on our condition for the possibility that a connection
            # is useable
            self._await_available_conn(remaining)

            # self.shutdown() may trigger the above Condition
            if self.is_shutdown:
                raise ConnectionException("Pool is shutdown")

            conns = self._connections
            if conns:
                least_busy = min(conns, key=lambda c: c.in_flight)
                with least_busy.lock:
                    if least_busy.in_flight < least_busy.max_request_id:
                        least_busy.in_flight += 1
                        return least_busy, least_busy.get_request_id()

            remaining = timeout - (time.time() - start)

        raise NoConnectionsAvailable() 
Example 8
Project: rtp_cluster   Author: sippy   File: Rtp_proxy_client_stream.py    BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __init__(self, global_config, address = '/var/run/rtpproxy.sock', \
      bind_address = None, nworkers = 1, family = socket.AF_UNIX):
        #print('Rtp_proxy_client_stream.__init__', address, bind_address, nworkers, family)
        if family == socket.AF_UNIX:
            self.is_local = True
            self.address = address
        else:
            self.is_local = False
            self.address = self.getdestbyaddr(address, family)
        self.family = family
        self.wi_available = Condition()
        self.wi = []
        self.nworkers = nworkers
        self.workers = []
        for i in range(0, self.nworkers):
            try:
                self.workers.append(_RTPPLWorker(self))
            except:
                break
        self.nworkers_act = i + 1
        self.delay_flt = recfilter(0.95, 0.25) 
Example 9
Project: jawfish   Author: war-and-code   File: queue.py    MIT License 6 votes vote down vote up
def __init__(self, maxsize=0):
        self.maxsize = maxsize
        self._init(maxsize)
        # mutex must be held whenever the queue is mutating.  All methods
        # that acquire mutex must release it before returning.  mutex
        # is shared between the three conditions, so acquiring and
        # releasing the conditions also acquires and releases mutex.
        self.mutex = _threading.Lock()
        # Notify not_empty whenever an item is added to the queue; a
        # thread waiting to get is notified then.
        self.not_empty = _threading.Condition(self.mutex)
        # Notify not_full whenever an item is removed from the queue;
        # a thread waiting to put is notified then.
        self.not_full = _threading.Condition(self.mutex)
        # Notify all_tasks_done whenever the number of unfinished tasks
        # drops to zero; thread waiting to join() is notified to resume
        self.all_tasks_done = _threading.Condition(self.mutex)
        self.unfinished_tasks = 0 
Example 10
Project: jawfish   Author: war-and-code   File: queue.py    MIT License 6 votes vote down vote up
def __init__(self, maxsize=0):
        self.maxsize = maxsize
        self._init(maxsize)

        # mutex must be held whenever the queue is mutating.  All methods
        # that acquire mutex must release it before returning.  mutex
        # is shared between the three conditions, so acquiring and
        # releasing the conditions also acquires and releases mutex.
        self.mutex = threading.Lock()

        # Notify not_empty whenever an item is added to the queue; a
        # thread waiting to get is notified then.
        self.not_empty = threading.Condition(self.mutex)

        # Notify not_full whenever an item is removed from the queue;
        # a thread waiting to put is notified then.
        self.not_full = threading.Condition(self.mutex)

        # Notify all_tasks_done whenever the number of unfinished tasks
        # drops to zero; thread waiting to join() is notified to resume
        self.all_tasks_done = threading.Condition(self.mutex)
        self.unfinished_tasks = 0 
Example 11
Project: wok   Author: bbglab   File: mcore.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, conf):
		JobManager.__init__(self, "mcore", conf)

		self._num_cores = self._conf.get("max_cores", mp.cpu_count(), dtype=int)

		self._running = False
		self._kill_threads = False
		self._threads = []

		self._run_lock = Lock()
		self._run_cvar = Condition(self._run_lock)

		self._jobs = {}

		#self._waiting_queue = PriorityQueue()

		self._task_output_files = {}

		# get hostname
		try:
			import socket
			self.hostname = socket.gethostname()
		except:
			self.hostname = "unknown" 
Example 12
Project: wok   Author: bbglab   File: mcore.py    GNU General Public License v3.0 6 votes vote down vote up
def _next_job(self, session):
		timeout = ProgressiveTimeout(0.5, 6.0, 0.5)
		job = None
		while job is None and self._running:
			try:
				with self._lock:
					job = session.query(McoreJob).filter(McoreJob.state == runstates.WAITING)\
													.order_by(McoreJob.priority).first()
					if job is not None:
						job.state = runstates.RUNNING
						session.commit()
			except Exception as e:
				self._log.exception(e)
				job = None
			if job is None and self._running:
				timeout.sleep() #TODO Guess how to use a Condition (self._run_cvar) or an Event

		return job 
Example 13
Project: JukeBox   Author: gauravsarkar97   File: Py25Queue.py    MIT License 6 votes vote down vote up
def __init__(self, maxsize=0):
        try:
            import threading
        except ImportError:
            import dummy_threading as threading
        self._init(maxsize)
        # mutex must be held whenever the queue is mutating.  All methods
        # that acquire mutex must release it before returning.  mutex
        # is shared between the three conditions, so acquiring and
        # releasing the conditions also acquires and releases mutex.
        self.mutex = threading.Lock()
        # Notify not_empty whenever an item is added to the queue; a
        # thread waiting to get is notified then.
        self.not_empty = threading.Condition(self.mutex)
        # Notify not_full whenever an item is removed from the queue;
        # a thread waiting to put is notified then.
        self.not_full = threading.Condition(self.mutex)
        # Notify all_tasks_done whenever the number of unfinished tasks
        # drops to zero; thread waiting to join() is notified to resume
        self.all_tasks_done = threading.Condition(self.mutex)
        self.unfinished_tasks = 0 
Example 14
Project: PythonClassBook   Author: PythonClassRoom   File: fig20_06.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__( self ):
      """Initialize variables and setup server"""
      
      HOST = ""
      PORT = 5000

      self.board = []
      self.currentPlayer = 0
      self.turnCondition = threading.Condition()
      self.gameBeginEvent = threading.Event()

      for i in range( 9 ):
         self.board.append( None )

      # setup server socket
      self.server = socket.socket( socket.AF_INET,
         socket.SOCK_STREAM )
      self.server.bind( ( HOST, PORT ) )
      self.display( "Server awaiting connections..." ) 
Example 15
Project: ConnectionPool   Author: zhouyl   File: __init__.py    MIT License 6 votes vote down vote up
def __init__(self, create, max_size=10, max_usage=0, ttl=0, idle=60, block=True):
        '''初始化参数

            create:    必须是一个可 callback 的函数
            max_size:  最大连接数,当为 0 的时候则没有限制,不建议设置为 0
            max_usage: 连接可使用次数,达到该次数后,连接将被释放/关闭
            ttl:       连接可使用时间,单位(秒),当连接使用达到指定时间后,连接将被释放/关闭
            idle:      连接空闲时间,单位(秒),当连接在闲置指定时间后,将被释放/关闭
            block:     当连接数满的时候,是否阻塞等待连接被释放,输入 False 则在连接池满时会抛出异常
        '''
        if not hasattr(create, '__call__'):
            raise ValueError('"create" argument is not callable')

        self._create = create
        self._max_size = int(max_size)
        self._max_usage = int(max_usage)
        self._ttl = int(ttl)
        self._idle = int(idle)
        self._block = bool(block)
        self._lock = threading.Condition()
        self._pool = queue.Queue()
        self._size = 0 
Example 16
Project: NiujiaoDebugger   Author: MrSrc   File: queue.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, maxsize=0):
        self.maxsize = maxsize
        self._init(maxsize)

        # mutex must be held whenever the queue is mutating.  All methods
        # that acquire mutex must release it before returning.  mutex
        # is shared between the three conditions, so acquiring and
        # releasing the conditions also acquires and releases mutex.
        self.mutex = threading.Lock()

        # Notify not_empty whenever an item is added to the queue; a
        # thread waiting to get is notified then.
        self.not_empty = threading.Condition(self.mutex)

        # Notify not_full whenever an item is removed from the queue;
        # a thread waiting to put is notified then.
        self.not_full = threading.Condition(self.mutex)

        # Notify all_tasks_done whenever the number of unfinished tasks
        # drops to zero; thread waiting to join() is notified to resume
        self.all_tasks_done = threading.Condition(self.mutex)
        self.unfinished_tasks = 0 
Example 17
Project: python-zhmcclient   Author: zhmcclient   File: _notification.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, handover_dict, handover_cond):
        """
        Parameters:

          handover_dict (dict): Dictionary for handing over the notification
            header and message from this listener thread to the receiver
            thread. Must initially be an empty dictionary.

          handover_cond (threading.Condition): Condition object for handing
            over the notification from this listener thread to the receiver
            thread. Must initially be a new threading.Condition object.
        """

        # Sync variables for thread-safe handover between listener thread and
        # receiver thread:
        self._handover_dict = handover_dict  # keys: headers, message
        self._handover_cond = handover_cond

        # Wait timeout to honor keyboard interrupts after this time:
        self._wait_timeout = 10.0  # seconds 
Example 18
Project: nyx   Author: sambuddhabasu   File: tracker.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, rate):
    super(Daemon, self).__init__()
    self.setDaemon(True)

    self._process_lock = threading.RLock()
    self._process_pid = None
    self._process_name = None

    self._rate = rate
    self._last_ran = -1  # time when we last ran
    self._run_counter = 0  # counter for the number of successful runs

    self._is_paused = False
    self._pause_condition = threading.Condition()
    self._halt = False  # terminates thread if true

    controller = tor_controller()
    controller.add_status_listener(self._tor_status_listener)
    self._tor_status_listener(controller, stem.control.State.INIT, None) 
Example 19
Project: pyblish-win   Author: pyblish   File: test_contextlib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def testWithCondition(self):
        lock = threading.Condition()
        def locked():
            return lock._is_owned()
        self.boilerPlate(lock, locked) 
Example 20
Project: pyblish-win   Author: pyblish   File: queues.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _after_fork(self):
        debug('Queue._after_fork()')
        self._notempty = threading.Condition(threading.Lock())
        self._buffer = collections.deque()
        self._thread = None
        self._jointhread = None
        self._joincancelled = False
        self._closed = False
        self._close = None
        self._send = self._writer.send
        self._recv = self._reader.recv
        self._poll = self._reader.poll 
Example 21
Project: pyblish-win   Author: pyblish   File: queues.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, maxsize=0):
        Queue.__init__(self, maxsize)
        self._unfinished_tasks = Semaphore(0)
        self._cond = Condition() 
Example 22
Project: pyblish-win   Author: pyblish   File: pool.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, cache, callback):
        self._cond = threading.Condition(threading.Lock())
        self._job = job_counter.next()
        self._cache = cache
        self._ready = False
        self._callback = callback
        cache[self._job] = self 
Example 23
Project: pyblish-win   Author: pyblish   File: rpc.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def asynccall(self, oid, methodname, args, kwargs):
        request = ("CALL", (oid, methodname, args, kwargs))
        seq = self.newseq()
        if threading.currentThread() != self.sockthread:
            cvar = threading.Condition()
            self.cvars[seq] = cvar
        self.debug(("asynccall:%d:" % seq), oid, methodname, args, kwargs)
        self.putmessage((seq, request))
        return seq 
Example 24
Project: pyblish-win   Author: pyblish   File: rpc.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def asyncqueue(self, oid, methodname, args, kwargs):
        request = ("QUEUE", (oid, methodname, args, kwargs))
        seq = self.newseq()
        if threading.currentThread() != self.sockthread:
            cvar = threading.Condition()
            self.cvars[seq] = cvar
        self.debug(("asyncqueue:%d:" % seq), oid, methodname, args, kwargs)
        self.putmessage((seq, request))
        return seq 
Example 25
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: pubSub.py    MIT License 5 votes vote down vote up
def run(self):
    while True:
      integer = random.randint(0,1000)
      self.condition.acquire()
      print("Condition Acquired by Publisher: {}".format(self.name))
      self.integers.append(integer)
      print("Publisher {} appending to array: {}".format(self.name, integer))
      self.condition.notify()
      print("Condition Released by Publisher: {}".format(self.name))
      self.condition.release()
      time.sleep(1) 
Example 26
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: pubSub.py    MIT License 5 votes vote down vote up
def run(self):
    while True:
      self.condition.acquire()
      print("Condition Acquired by Consumer: {}".format(self.name))
      while True:
        if self.integers:
          integer = self.integers.pop()
          print("{} Popped from list by Consumer: {}".format(integer, self.name))
          break
        print("Condition Wait by {}".format(self.name))
        self.condition.wait()
      print("Consumer {} Releasing Condition".format(self.name))
      self.condition.release() 
Example 27
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: producerConsumer.py    MIT License 5 votes vote down vote up
def main():
    integers = []
    condition = threading.Condition()
    t1 = Producer(integers, condition)
    t2 = Consumer(integers, condition)
    t1.start()
    t2.start()
    t1.join()
    t2.join() 
Example 28
Project: pyServer   Author: archanpatkar   File: TSQueue.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.q = [];
        self.monitor = threading.Condition(); 
Example 29
Project: bigquerylayers   Author: smandaric   File: bidi.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, bidi_rpc, on_response):
        self._bidi_rpc = bidi_rpc
        self._on_response = on_response
        self._paused = False
        self._wake = threading.Condition()
        self._thread = None
        self._operational_lock = threading.Lock() 
Example 30
Project: AshsSDK   Author: thehappydinoa   File: utils.py    MIT License 5 votes vote down vote up
def __init__(self, count):
        self._count = count
        # Dict[tag, next_sequence_number].
        self._tag_sequences = defaultdict(int)
        self._lowest_sequence = {}
        self._lock = threading.Lock()
        self._condition = threading.Condition(self._lock)
        # Dict[tag, List[sequence_number]]
        self._pending_release = {} 
Example 31
Project: AshsSDK   Author: thehappydinoa   File: _base.py    MIT License 5 votes vote down vote up
def __init__(self):
        """Initializes the future. Should not be called by clients."""
        self._condition = threading.Condition()
        self._state = PENDING
        self._result = None
        self._exception = None
        self._traceback = None
        self._waiters = []
        self._done_callbacks = [] 
Example 32
Project: Cryptchat   Author: djohsson   File: networkhandler.py    MIT License 5 votes vote down vote up
def __init__(self, address, port, servermode, dh):
        self.address = address
        self.port = port
        self.servermode = servermode
        self.dh = dh
        self.exchangedkeys = Event()
        self.starter = NetworkStarter(self)

        self.in_message = []
        self.in_condition = Condition()
        self.in_sem = Semaphore(0)

        self.out_message = []
        self.out_condition = Condition()
        self.out_sem = Semaphore(0) 
Example 33
Project: Distributed-AutoDeepLab   Author: HankKung   File: comm.py    Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self._result = None
        self._lock = threading.Lock()
        self._cond = threading.Condition(self._lock) 
Example 34
Project: openhatch   Author: campbe13   File: pool.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, cache):
        self._cond = threading.Condition(threading.Lock())
        self._job = job_counter.next()
        self._cache = cache
        self._items = collections.deque()
        self._index = 0
        self._length = None
        self._unsorted = {}
        cache[self._job] = self 
Example 35
Project: openhatch   Author: campbe13   File: timer2.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, schedule=None, on_error=None, on_tick=None, **kwargs):
        self.schedule = schedule or self.Schedule(on_error=on_error)
        self.on_tick = on_tick or self.on_tick

        Thread.__init__(self)
        self._is_shutdown = Event()
        self._is_stopped = Event()
        self.mutex = Lock()
        self.logger = logging.getLogger("timer2.Timer")
        self.not_empty = Condition(self.mutex)
        self.setDaemon(True)
        self.setName("Timer-%s" % (self._timer_count(), )) 
Example 36
Project: openhatch   Author: campbe13   File: buckets.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, task_registry):
        self.task_registry = task_registry
        self.buckets = {}
        self.init_with_registry()
        self.immediate = deque()
        self.mutex = threading.Lock()
        self.not_empty = threading.Condition(self.mutex) 
Example 37
Project: king-phisher-plugins   Author: securestate   File: tasks.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self):
		self.mutex = threading.RLock()
		self.not_empty = threading.Condition(self.mutex)
		self.not_full = threading.Condition(self.mutex)
		self.queue = []
		self.unfinished_tasks = 0 
Example 38
Project: deb-python-cassandra-driver   Author: openstack   File: concurrent.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, session, statements_and_params):
        self.session = session
        self._enum_statements = enumerate(iter(statements_and_params))
        self._condition = Condition()
        self._fail_fast = False
        self._results_queue = []
        self._current = 0
        self._exec_count = 0
        self._exec_depth = 0 
Example 39
Project: rtp_cluster   Author: sippy   File: DNRelay.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, spath, sip_logger):
        if spath.startswith('unix:'):
            spath = spath[5:]
        self.spath = spath
        self.sip_logger = sip_logger
        self.wi_available = Condition()
        self.wi = []
        Thread.__init__(self)
        self.setDaemon(True)
        self.start() 
Example 40
Project: rtp_cluster   Author: sippy   File: CLIManager.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, clientsock, clim):
        #print(self.__init__)
        Thread.__init__(self)
        self.clientsock = clientsock
        self.clim = clim
        self.w_available = Condition()
        self.wbuffer = bytes()
        self.setDaemon(True)
        self.start() 
Example 41
Project: rtp_cluster   Author: sippy   File: Udp_server.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, global_config, uopts):
        self.uopts = uopts.getCopy()
        self.skt = socket.socket(self.uopts.family, socket.SOCK_DGRAM)
        if self.uopts.laddress != None:
            ai = socket.getaddrinfo(self.uopts.laddress[0], None, self.uopts.family)
            if self.uopts.family == socket.AF_INET:
                address = (ai[0][4][0], self.uopts.laddress[1])
            else:
                address = (ai[0][4][0], self.uopts.laddress[1], ai[0][4][2], ai[0][4][3])
            if (self.uopts.flags & socket.SO_REUSEADDR) != 0:
                self.skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            if hasattr(socket, 'SO_REUSEPORT') and \
              (self.uopts.flags & socket.SO_REUSEPORT) != 0:
                self.skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
            self.skt.bind(address)
            if self.uopts.laddress[1] == 0:
                self.uopts.laddress = self.skt.getsockname()
        self.sendqueue = []
        self.stats = [0, 0, 0]
        self.wi_available = Condition()
        self.wi = []
        self.asenders = []
        self.areceivers = []
        if self.uopts.nworkers == None:
            nworkers = _DEFAULT_NWORKERS
        else:
            nworkers = self.uopts.nworkers
        for i in range(0, nworkers):
            self.asenders.append(AsyncSender(self))
            self.areceivers.append(AsyncReceiver(self)) 
Example 42
Project: rtp_cluster   Author: sippy   File: SipLogger.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, app, call_id = 'GLOBAL', logfile = '/var/log/sip.log'):
        self.itime = time()
        self.app = '/%s' % app
        self.call_id = call_id
        bend = os.environ.get('SIPLOG_BEND', 'stderr').lower()
        tform = os.environ.get('SIPLOG_TFORM', 'abs').lower()
        if tform == 'rel':
            self.offstime = True
            itime = os.environ.get('SIPLOG_TSTART', self.itime)
            self.itime = float(itime)
        self.level = eval('SIPLOG_' + os.environ.get('SIPLOG_LVL', 'INFO'))
        if bend == 'stderr':
            self.write = self.write_stderr
        elif bend == 'none':
            self.write = self.donoting
        else:
            self.write = self.write_logfile
            self.wi_available = Condition()
            self.wi = []
            if bend != 'syslog':
                self.logger = AsyncLogger(app, self)
                self.logfile = os.environ.get('SIPLOG_LOGFILE_FILE', logfile)
                self.signal_handler = LogSignal(self, SIGUSR1, self.reopen)
            else:
                self.logger = AsyncLoggerSyslog(app, self)
                self.app = '' 
Example 43
Project: overhaul-distillation   Author: clovaai   File: comm.py    MIT License 5 votes vote down vote up
def __init__(self):
        self._result = None
        self._lock = threading.Lock()
        self._cond = threading.Condition(self._lock) 
Example 44
Project: bitpay-brick   Author: javgh   File: tco.py    MIT License 5 votes vote down vote up
def __init__(self, send_miu, recv_miu):
        self.lock = threading.RLock()
        self.mode = TransmissionControlObject.Mode()
        self.state = TransmissionControlObject.State()
        self.send_queue = collections.deque()
        self.recv_queue = collections.deque()
        self.send_ready = threading.Condition(self.lock)
        self.recv_ready = threading.Condition(self.lock)
        self.recv_miu = recv_miu
        self.send_miu = send_miu
        self.recv_buf = 1
        self.send_buf = 1
        self.addr = None
        self.peer = None 
Example 45
Project: bitpay-brick   Author: javgh   File: tco.py    MIT License 5 votes vote down vote up
def __init__(self, recv_miu, recv_win):
        super(DataLinkConnection, self).__init__(128, recv_miu)
        self.state.CLOSED = True
        self.acks_ready = threading.Condition(self.lock)
        self.acks_recvd = 0 # received acknowledgements
        self.recv_confs = 0 # outstanding receive confirmations
        self.send_token = threading.Condition(self.lock)
        self.recv_buf = recv_win
        self.recv_win = recv_win # RW(Local)
        self.recv_cnt = 0        # V(R)
        self.recv_ack = 0        # V(RA)
        self.send_win = None     # RW(Remote)
        self.send_cnt = 0        # V(S)
        self.send_ack = 0        # V(SA) 
Example 46
Project: bitpay-brick   Author: javgh   File: llc.py    MIT License 5 votes vote down vote up
def __init__(self, llc):
        self.llc = llc
        self.snl = dict()
        self.tids = range(256)
        self.resp = threading.Condition(self.llc.lock)
        self.sent = dict()
        self.sdreq = collections.deque()
        self.sdres = collections.deque()
        self.dmpdu = collections.deque() 
Example 47
Project: jawfish   Author: war-and-code   File: pool.py    MIT License 5 votes vote down vote up
def __init__(self, cache):
        self._cond = threading.Condition(threading.Lock())
        self._job = next(job_counter)
        self._cache = cache
        self._items = collections.deque()
        self._index = 0
        self._length = None
        self._unsorted = {}
        cache[self._job] = self 
Example 48
Project: wok   Author: bbglab   File: atomic.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, initial_value=0):
		self._lock = Lock()
		self._cvar = Condition(self._lock)
		self._value = initial_value 
Example 49
Project: lnd_grpc   Author: willcl-ark   File: utils.py    MIT License 5 votes vote down vote up
def __init__(self, outputDir=None, prefix="proc"):
        self.logs = []
        self.logs_cond = threading.Condition(threading.RLock())
        self.cmd_line = None
        self.running = False
        self.proc = None
        self.outputDir = outputDir
        self.logger = logging.getLogger(prefix) 
Example 50
Project: exposure   Author: yuanming-hu   File: util.py    MIT License 5 votes vote down vote up
def __init__(self, target, args=(), kwargs={}):
    self.target = target
    self.args = args
    self.kwargs = kwargs
    self.condition = threading.Condition()
    self.result = None
    self.thread = threading.Thread(target=self.worker)
    self.stopped = False
    self.thread.daemon = True
    self.thread.start() 
Example 51
Project: RedditImageDownloader   Author: sarvothaman   File: hitem.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self,event):
        logger = logging.getLogger('Reddit_API_Hitter')
        logger.setLevel(logging.DEBUG)
        # create file handler which logs even debug messages
        fh = logging.FileHandler('logs/reddit_api.log')
        fh.setLevel(logging.DEBUG)
        # create console handler with a higher log level
        ch = logging.StreamHandler()
        ch.setLevel(logging.ERROR)
        # create formatter and add it to the handlers
        formatter = logging.Formatter('[%(filename)s:%(lineno)s - %(funcName)20s() ] %(asctime)s %(levelname)s:%(message)s')
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)
        # add the handlers to the logger
        logger.addHandler(fh)
        logger.addHandler(ch)
        logger.debug("-------------------------------")
        threading.Thread.__init__(self,daemon=False)
        self.stopped=event
        self.urllist=RedditURL()
        self.requestLeft=30
        self.limit=25
        self.connectSession=requests.Session()
        self.connectSession.headers.update({'User-Agent':'python3_4/win8_64:RedditDownloader:v0.1b(by /u/reladric)'})
        self.lock= threading.Lock()
        self.conditionObj=threading.Condition(self.lock) 
Example 52
Project: PythonClassBook   Author: PythonClassRoom   File: fig19_11.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__( self, threadName, semaphore ):
        "Initialize thread"

        threading.Thread.__init__( self, name = threadName )
        self.sleepTime = random.randrange( 1, 6 )

        # set the semaphore as a data attribute of the class
        self.threadSemaphore = semaphore

	# allow only one thread to access threadCount at a time
	self.threadCondition = threading.Condition() 
Example 53
Project: PythonClassBook   Author: PythonClassRoom   File: CircularBuffer.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__( self ):
      """Set buffer, count, locations and condition variable"""

      # each element in list is a buffer      
      self.buffer = [ -1, -1, -1 ]
      
      self.occupiedBufferCount = 0   # count of occupied buffers
      self.readLocation = 0          # current reading index
      self.writeLocation = 0         # current writing index
      
      self.threadCondition = threading.Condition() 
Example 54
Project: PythonClassBook   Author: PythonClassRoom   File: SynchronizedInteger.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__( self ):
      """Initialize integer, buffer count and condition variable"""
      
      self.buffer = -1  
      self.occupiedBufferCount = 0   # number of occupied buffers
      self.threadCondition = threading.Condition() 
Example 55
Project: misp42splunk   Author: remg427   File: rwlock.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self._condition = threading.Condition(threading.Lock())
        self._readers = 0 
Example 56
Project: DRCOG_Urbansim   Author: apdjustino   File: model_system.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.running = False
        self.forked_processes = []
        self.running_conditional = threading.Condition() 
Example 57
Project: RefineDet_MxNet   Author: DetectionTeamUCAS   File: syncbn.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, key, nchannels, num_devices):
        self._mutex = threading.Lock()
        self._all_tasks_done = threading.Condition(self._mutex)
        self._key = key
        self.num_devices = int(num_devices)
        self.out = mx.nd.zeros(nchannels)
        self._clear() 
Example 58
Project: cproto   Author: asyne   File: websocket.py    MIT License 5 votes vote down vote up
def __init__(self, on_event, *args, **kwargs):
        super(self.__class__, self).__init__(*args, **kwargs)

        self._on_event = on_event

        self.request_id = 1
        self.cLock = threading.Condition()
        self.replies = Replies()
        self.events = Queue() 
Example 59
Project: Make_Money_with_Tensorflow   Author: llSourcell   File: mnist_client.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, num_tests, concurrency):
    self._num_tests = num_tests
    self._concurrency = concurrency
    self._error = 0
    self._done = 0
    self._active = 0
    self._condition = threading.Condition() 
Example 60
Project: Flask_Blog   Author: sugarguo   File: fcgi_base.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, conn):
        super(MultiplexedInputStream, self).__init__(conn)

        # Arbitrates access to this InputStream (it's used simultaneously
        # by a Request and its owning Connection object).
        lock = threading.RLock()

        # Notifies Request thread that there is new data available.
        self._lock = threading.Condition(lock) 
Example 61
Project: Flask_Blog   Author: sugarguo   File: fcgi_base.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, sock, addr, server):
        super(MultiplexedConnection, self).__init__(sock, addr, server)

        # Used to arbitrate access to self._requests.
        lock = threading.RLock()

        # Notification is posted everytime a request completes, allowing us
        # to quit cleanly.
        self._lock = threading.Condition(lock) 
Example 62
Project: Flask_Blog   Author: sugarguo   File: threadpool.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, minSpare=1, maxSpare=5, maxThreads=sys.maxint):
        self._minSpare = minSpare
        self._maxSpare = maxSpare
        self._maxThreads = max(minSpare, maxThreads)

        self._lock = threading.Condition()
        self._workQueue = []
        self._idleCount = self._workerCount = maxSpare

        # Start the minimum number of worker threads.
        for i in range(maxSpare):
            thread.start_new_thread(self._worker, ()) 
Example 63
Project: surreptitious   Author: neuroFuzz   File: queue.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _init(self, maxsize):
        self.mutex = threading.Lock()
        self.not_empty = threading.Condition(self.mutex)
        self.not_full = threading.Condition(self.mutex)
        self.all_tasks_done = threading.Condition(self.mutex)

        if not os.path.exists(self.path):
            os.makedirs(self.path)
            self.protocol = common.select_pickle_protocol() 
Example 64
Project: siphon-cli   Author: getsiphon   File: delayed_queue.py    MIT License 5 votes vote down vote up
def __init__(self, delay):
        self.delay = delay
        self._lock = threading.Lock()
        self._not_empty = threading.Condition(self._lock)
        self._queue = deque()
        self._closed = False 
Example 65
Project: siphon-cli   Author: getsiphon   File: event_backport.py    MIT License 5 votes vote down vote up
def __init__(self,):
        self.__cond = Condition(Lock())
        self.__flag = False 
Example 66
Project: NiujiaoDebugger   Author: MrSrc   File: _base.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        """Initializes the future. Should not be called by clients."""
        self._condition = threading.Condition()
        self._state = PENDING
        self._result = None
        self._exception = None
        self._waiters = []
        self._done_callbacks = [] 
Example 67
Project: NiujiaoDebugger   Author: MrSrc   File: test_contextlib.py    GNU General Public License v3.0 5 votes vote down vote up
def testWithCondition(self):
        lock = threading.Condition()
        def locked():
            return lock._is_owned()
        self.boilerPlate(lock, locked) 
Example 68
Project: NiujiaoDebugger   Author: MrSrc   File: test_sched.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self._cond = threading.Condition()
        self._time = 0
        self._stop = 0 
Example 69
Project: NiujiaoDebugger   Author: MrSrc   File: queues.py    GNU General Public License v3.0 5 votes vote down vote up
def _after_fork(self):
        debug('Queue._after_fork()')
        self._notempty = threading.Condition(threading.Lock())
        self._buffer = collections.deque()
        self._thread = None
        self._jointhread = None
        self._joincancelled = False
        self._closed = False
        self._close = None
        self._send_bytes = self._writer.send_bytes
        self._recv_bytes = self._reader.recv_bytes
        self._poll = self._reader.poll 
Example 70
Project: NiujiaoDebugger   Author: MrSrc   File: queues.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, maxsize=0, *, ctx):
        Queue.__init__(self, maxsize, ctx=ctx)
        self._unfinished_tasks = ctx.Semaphore(0)
        self._cond = ctx.Condition() 
Example 71
Project: NiujiaoDebugger   Author: MrSrc   File: rpc.py    GNU General Public License v3.0 5 votes vote down vote up
def asynccall(self, oid, methodname, args, kwargs):
        request = ("CALL", (oid, methodname, args, kwargs))
        seq = self.newseq()
        if threading.current_thread() != self.sockthread:
            cvar = threading.Condition()
            self.cvars[seq] = cvar
        self.debug(("asynccall:%d:" % seq), oid, methodname, args, kwargs)
        self.putmessage((seq, request))
        return seq 
Example 72
Project: NiujiaoDebugger   Author: MrSrc   File: rpc.py    GNU General Public License v3.0 5 votes vote down vote up
def asyncqueue(self, oid, methodname, args, kwargs):
        request = ("QUEUE", (oid, methodname, args, kwargs))
        seq = self.newseq()
        if threading.current_thread() != self.sockthread:
            cvar = threading.Condition()
            self.cvars[seq] = cvar
        self.debug(("asyncqueue:%d:" % seq), oid, methodname, args, kwargs)
        self.putmessage((seq, request))
        return seq 
Example 73
Project: cimon_controller   Author: SchweizerischeBundesbahnen   File: clewareampeloutput.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, device=None, flash_interval_sec=default_flash_interval_sec, absoulte_every_sec=default_absoulte_every_sec):
        self.device=device
        self.flash_interval_sec = flash_interval_sec
        self.to_display = (False, False, False)
        self.__stopped = True
        self.condition = Condition() # condition has its own (r)lock
        self.udpated = False
        self.current_display = (None, None, None)
        self.absolute_every = timedelta(seconds=absoulte_every_sec)
        self.__absolute_next = datetime.now() + self.absolute_every 
Example 74
Project: cimon_controller   Author: SchweizerischeBundesbahnen   File: collector.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, username, password, login_url, ssl_config=SslConfig()):
        self.login_http_client = HttpClient(login_url, BasicAuthenticationHandler(username, password), ssl_config=ssl_config)
        self.token = None
        self.is_renewing = False
        self.renewing = Condition() 
Example 75
Project: sinfo-rpi-truck   Author: tblxio   File: cameraRunner.py    MIT License 5 votes vote down vote up
def __init__(self):
        self.frame = None
        self.buffer = io.BytesIO()
        self.condition = Condition() 
Example 76
Project: nyx   Author: sambuddhabasu   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, update_rate):
    Panel.__init__(self)
    threading.Thread.__init__(self)
    self.setDaemon(True)

    self._pause_condition = threading.Condition()
    self._halt = False  # terminates thread if true
    self._update_rate = update_rate 
Example 77
Project: recruit   Author: Frank-qlu   File: thread_util.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, value=1):
        if value < 0:
            raise ValueError("semaphore initial value must be >= 0")
        self._cond = threading.Condition(threading.Lock())
        self._value = value 
Example 78
Project: sparkflow   Author: lifeomic   File: RWLock.py    MIT License 5 votes vote down vote up
def __init__(self):
        self.rwlock = 0
        self.writers_waiting = 0
        self.monitor = threading.Lock()
        self.readers_ok = threading.Condition(self.monitor)
        self.writers_ok = threading.Condition(self.monitor) 
Example 79
Project: bigquerylayers   Author: smandaric   File: bidi.py    GNU General Public License v3.0 4 votes vote down vote up
def _thread_main(self, ready):
        try:
            ready.set()
            self._bidi_rpc.add_done_callback(self._on_call_done)
            self._bidi_rpc.open()

            while self._bidi_rpc.is_active:
                # Do not allow the paused status to change at all during this
                # section. There is a condition where we could be resumed
                # between checking if we are paused and calling wake.wait(),
                # which means that we will miss the notification to wake up
                # (oops!) and wait for a notification that will never come.
                # Keeping the lock throughout avoids that.
                # In the future, we could use `Condition.wait_for` if we drop
                # Python 2.7.
                with self._wake:
                    while self._paused:
                        _LOGGER.debug("paused, waiting for waking.")
                        self._wake.wait()
                        _LOGGER.debug("woken.")

                _LOGGER.debug("waiting for recv.")
                response = self._bidi_rpc.recv()
                _LOGGER.debug("recved response.")
                self._on_response(response)

        except exceptions.GoogleAPICallError as exc:
            _LOGGER.debug(
                "%s caught error %s and will exit. Generally this is due to "
                "the RPC itself being cancelled and the error will be "
                "surfaced to the calling code.",
                _BIDIRECTIONAL_CONSUMER_NAME,
                exc,
                exc_info=True,
            )

        except Exception as exc:
            _LOGGER.exception(
                "%s caught unexpected exception %s and will exit.",
                _BIDIRECTIONAL_CONSUMER_NAME,
                exc,
            )

        _LOGGER.info("%s exiting", _BIDIRECTIONAL_CONSUMER_NAME) 
Example 80
Project: NiujiaoDebugger   Author: MrSrc   File: ccbench.py    GNU General Public License v3.0 4 votes vote down vote up
def run_throughput_test(func, args, nthreads):
    assert nthreads >= 1

    # Warm up
    func(*args)

    results = []
    loop = TimedLoop(func, args)
    end_event = []

    if nthreads == 1:
        # Pure single-threaded performance, without any switching or
        # synchronization overhead.
        start_time = time.time()
        results.append(loop(start_time, THROUGHPUT_DURATION,
                            end_event, do_yield=False))
        return results

    started = False
    ready_cond = threading.Condition()
    start_cond = threading.Condition()
    ready = []

    def run():
        with ready_cond:
            ready.append(None)
            ready_cond.notify()
        with start_cond:
            while not started:
                start_cond.wait()
        results.append(loop(start_time, THROUGHPUT_DURATION,
                            end_event, do_yield=True))

    threads = []
    for i in range(nthreads):
        threads.append(threading.Thread(target=run))
    for t in threads:
        t.setDaemon(True)
        t.start()
    # We don't want measurements to include thread startup overhead,
    # so we arrange for timing to start after all threads are ready.
    with ready_cond:
        while len(ready) < nthreads:
            ready_cond.wait()
    with start_cond:
        start_time = time.time()
        started = True
        start_cond.notify(nthreads)
    for t in threads:
        t.join()

    return results