Python Queue.Empty() Examples

The following are 30 code examples of Queue.Empty(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module Queue , or try the search function .
Example #1
Source File: multiThreadDownload.py    From ThesaurusSpider with MIT License 6 votes vote down vote up
def run(self):
        global VISITED, DOWNLOADED, QUEUE
        while True:
            try:
                currentURL = QUEUE.get()
            except Queue.Empty:
                continue

            lock.acquire()  # 获取锁来修改VISITED内容
            try:
                if currentURL in VISITED:
                    QUEUE.task_done()
                    continue
                else:
                    VISITED.append(currentURL)
            finally:
                lock.release()

            try:
                response = urllib2.urlopen(currentURL)
                data = response.read()
            except urllib2.HTTPError, e:    #将可能发生的错误记录到日志文件中
                with open(DOWNLOADLOG, 'a') as f:
                    f.write(str(e.code)+' error while parsing the URL:'+currentURL+'\n')
            except: 
Example #2
Source File: core.py    From knob with MIT License 6 votes vote down vote up
def recvPacket(self, timeout=None):
        """
        This function polls the recvQueue for the next available HCI
        packet and returns it. The function checks whether it is called
        from the sendThread or any other thread and respectively chooses
        either the sendThreadrecvQueue or the recvQueue.

        The recvQueue is filled by the recvThread. If the queue fills up
        the recvThread empties the queue (unprocessed packets are lost).
        The recvPacket function is meant to receive raw HCI packets in
        a blocking manner. Consider using the registerHciCallback()
        functionality as an alternative which works asynchronously.
        """

        if not self.check_running():
            return None

        try:
            return self.recvQueue.get(timeout=timeout)
        except Queue.Empty:
            return None 
Example #3
Source File: core.py    From knob with MIT License 6 votes vote down vote up
def sendHciCommand(self, opcode, data, timeout=2):
        """
        Send an arbitrary HCI packet by pushing a send-task into the
        sendQueue. This function blocks until the response is received
        or the timeout expires. The return value is the Payload of the
        HCI Command Complete Event which was received in response to
        the command or None if no response was received within the timeout.
        """

        queue = Queue.Queue(1)
        try:
            self.sendQueue.put((opcode, data, queue), timeout=timeout)
            return queue.get(timeout=timeout)
        except Queue.Empty:
            log.warn("sendHciCommand: waiting for response timed out!")
            return None
        except Queue.Full:
            log.warn("sendHciCommand: send queue is full!")
            return None 
Example #4
Source File: multiproc_data.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def reset(self):
        """
        Resets the generator by stopping all processes
        """
        self.alive.value = False
        qsize = 0
        try:
            while True:
                self.queue.get(timeout=0.1)
                qsize += 1
        except QEmptyExcept:
            pass
        print("Queue size on reset: {}".format(qsize))
        for i, p in enumerate(self.proc):
            p.join()
        self.proc.clear() 
Example #5
Source File: core.py    From knob with MIT License 6 votes vote down vote up
def launchRam(self, address):
        """
        Executes a function at the specified address in the context of the HCI
        handler thread. The function has to comply with the calling convention.
        As the function blocks the HCI handler thread, the chip will most likely
        crash (or be resetted by Android) if the function takes too long.
        """
        

        response = self.sendHciCommand(0xfc4e, p32(address))
        if (response == None):
            log.warn("Empty HCI response during launchRam, driver crashed due to invalid code or destination")
            return False

        if(response[3] != '\x00'):
            log.warn("Got error code %x in command complete event." % response[3])
            return False
        
        # Nexus 6P Bugfix
        if ('LAUNCH_RAM_PAUSE' in dir(fw) and fw.LAUNCH_RAM_PAUSE):
            log.debug("launchRam: Bugfix, sleeping %ds" % fw.LAUNCH_RAM_PAUSE)
            time.sleep(fw.LAUNCH_RAM_PAUSE)
            
        return True 
Example #6
Source File: get_s3_stats.py    From hsds with Apache License 2.0 6 votes vote down vote up
def get_sizes(jsn):
   thrd = threading.current_thread()
   logging.info('starting thread '+str(thrd.ident)+' ...')
   try:
      while True:
         if queue.empty() == True: 
            break
         itm = queue.get()
         logging.info(str(thrd.ident)+' :' +str(itm))
         val = get_remote_size(itm)
         if val != None: jsn[itm]['objsize'] = val
         queue.task_done()
   except Queue.Empty: 
      pass
   logging.info('thread '+str(thrd.ident)+' done...') 
#get_sizes

#--------------------------------------------------------------------------------- 
Example #7
Source File: camera_node.py    From RacingRobot with MIT License 6 votes vote down vote up
def extractInfo(self):
        try:
            while not self.exit:
                try:
                    frame = self.frame_queue.get(block=True, timeout=1)
                except queue.Empty:
                    print("Queue empty")
                    continue
                try:
                    # Publish new image
                    msg = self.bridge.cv2_to_imgmsg(frame, 'rgb8')
                    if not self.exit:
                        self.image_publisher.publish(msg)
                except CvBridgeError as e:
                    print("Error Converting cv image: {}".format(e.message))
                self.frame_num += 1
        except Exception as e:
            print("Exception after loop: {}".format(e))
            raise 
Example #8
Source File: timer_queue.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _check_and_execute(self):
        wakeup_queue = self._wakeup_queue
        while 1:
            (next_expired_time, expired_timers) = self._get_expired_timers()
            for timer in expired_timers:
                try:
                    # Note, please make timer callback effective/short
                    timer()
                except Exception:
                    logging.error(traceback.format_exc())

            self._reset_timers(expired_timers)

            sleep_time = _calc_sleep_time(next_expired_time)
            try:
                wakeup = wakeup_queue.get(timeout=sleep_time)
                if wakeup is TEARDOWN_SENTINEL:
                    break
            except Queue.Empty:
                pass
        logging.info('TimerQueue stopped.') 
Example #9
Source File: ProxyManage.py    From Pansidong with GNU General Public License v3.0 6 votes vote down vote up
def _check_ip_all(self):
        rows = self.session.query(Proxy).all()
        self.thread_pool = ThreadPool(thread_count=10 if not len(rows)/20 else len(rows)/20)
        for row in rows:
            self.thread_pool.add_func(self._check, ip=row.ip, port=row.port, save_to_queue=True)
        self.thread_pool.close()
        self.thread_pool.join()
        while True:
            if self.thread_pool.exit is True and self.result_queue.empty():
                break
            else:
                try:
                    res = self.result_queue.get_nowait()
                    ip = res[0]
                    port = res[1]
                    delay = res[3]
                    alive = res[2]
                    logger.info("IP {0} Connect {1}, time: {2:.2f}s".format(ip, "success", delay)) if alive \
                        else logger.error("IP {0} Connect failed.".format(ip))
                    self._update_db(ip, port, delay, alive)
                except Queue.Empty:
                    time.sleep(2) 
Example #10
Source File: log_client.py    From iSDX with Apache License 2.0 6 votes vote down vote up
def flow_mod_sender(self):
        while self.run:
            try:
                flow_mod = self.flow_mod_queue.get(True, 0.5)
            except Empty:
                continue

            if self.timing:
                if self.simulation_start_time == 0:
                    self.real_start_time = time()
                    self.simulation_start_time = flow_mod["time"]

                sleep_time = self.sleep_time(flow_mod["time"])

                self.logger.debug('sleep for ' + str(sleep_time) + ' seconds')

                sleep(sleep_time)

            self.send(flow_mod) 
Example #11
Source File: shapenet.py    From lsm with MIT License 6 votes vote down vote up
def fetch_data(self, smids, items, im_batch):
        with self.coord.stop_on_exception():
            while not self.coord.should_stop():
                data = {}
                try:
                    data_idx = self.queue_idx.get(timeout=0.5)
                except Empty:
                    self.logger.debug('Index queue empty - {:s}'.format(
                        current_thread().name))
                    continue

                view_idx = np.random.choice(
                    self.num_renders, size=(im_batch, ), replace=False)
                sid, mid = smids[data_idx]
                for i in items:
                    data[i] = self.load_func[i](sid, mid, view_idx)

                self.queue_data.put(data)
                if self.loop_data:
                    self.queue_idx.put(data_idx) 
Example #12
Source File: ProxyConsumer.py    From django-angularjs-blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def run(self):
        while True:
            try:
                resource = self.q.get(block=True, timeout=3)
                spider = self.factory.create_spider(resource)
                addr_list = spider.run()
                self.nq.put(len(addr_list), block=True, timeout=3)
                for addr in addr_list:
                    self.cq.put(addr, block=True, timeout=3)
                    # print addr
                self.q.task_done()
            except Queue.Empty:
                break
            except Exception, e:
                print e
                pass 
Example #13
Source File: rmCommandThread.py    From rainmachine-developer-resources with GNU General Public License v3.0 6 votes vote down vote up
def doHandleMessages(self, limit = None):
        if not limit is None:
            messageCount = 0

        while True:
            try:
                command = self.messageQueue.get(True, self.waitTimeout)
                if command.name == "shutdown":
                    return False
                else:
                    self.doExecuteCommand(command)

                    if not limit is None:
                        messageCount += 1
                        if limit <= messageCount:
                            break

            except Empty, e:
                break
            except Exception, e:
                log.error(e) 
Example #14
Source File: adaptive_thread_pool.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def Shutdown(self):
    """Shutdown the thread pool.

    Tasks may remain unexecuted in the submit queue.
    """

    while not self.requeue.empty():
      try:
        unused_item = self.requeue.get_nowait()
        self.requeue.task_done()
      except Queue.Empty:

        pass
    for thread in self.__threads:
      thread.exit_flag = True
      self.requeue.put(_THREAD_SHOULD_EXIT)
    self.__thread_gate.EnableAllThreads() 
Example #15
Source File: library.py    From plugin.video.emby with GNU General Public License v3.0 6 votes vote down vote up
def run(self):

        while True:

            try:
                item = self.queue.get(timeout=3)
            except Queue.Empty:
                break

            time = self.music_time if item[0] == 'Audio' else self.video_time

            if time and (not self.player.isPlayingVideo() or xbmc.getCondVisibility('VideoPlayer.Content(livetv)')):
                dialog("notification", heading="%s %s" % (_(33049), item[0]), message=item[1],
                       icon="{emby}", time=time, sound=False)

            self.queue.task_done()

            if window('emby_should_stop.bool'):
                break

        LOG.info("--<[ q:notify/%s ]", id(self))
        self.is_done = True 
Example #16
Source File: timer_queue.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _check_and_execute(self):
        wakeup_queue = self._wakeup_queue
        while 1:
            (next_expired_time, expired_timers) = self._get_expired_timers()
            for timer in expired_timers:
                try:
                    # Note, please make timer callback effective/short
                    timer()
                except Exception:
                    logging.error(traceback.format_exc())

            self._reset_timers(expired_timers)

            sleep_time = _calc_sleep_time(next_expired_time)
            try:
                wakeup = wakeup_queue.get(timeout=sleep_time)
                if wakeup is TEARDOWN_SENTINEL:
                    break
            except Queue.Empty:
                pass
        logging.info('TimerQueue stopped.') 
Example #17
Source File: record_processor.py    From GreenPiThumb with Apache License 2.0 5 votes vote down vote up
def try_process_next_record(self):
        """Processes the next record from the queue, placing it in a store.

        If an item is available in the queue, removes it and places it in the
        appropriate store. If no item is in the queue, returns immediately.

        Must be called from the same thread from which the database connections
        were created.

        Returns:
            True if it processed a record, False if the queue contained no
            records.

        Raises:
            UnsupportedRecordError if the queue contains an unexpected record
                type.
        """
        try:
            record = self._record_queue.get_nowait()
        except Queue.Empty:
            return False

        if isinstance(record, db_store.SoilMoistureRecord):
            self._soil_moisture_store.insert(record)
        elif isinstance(record, db_store.LightRecord):
            self._light_store.insert(record)
        elif isinstance(record, db_store.HumidityRecord):
            self._humidity_store.insert(record)
        elif isinstance(record, db_store.TemperatureRecord):
            self._temperature_store.insert(record)
        elif isinstance(record, db_store.WateringEventRecord):
            self._watering_event_store.insert(record)
        else:
            raise UnsupportedRecordError(
                'Unrecognized record type: %s' % str(record))
        return True 
Example #18
Source File: CheckConsumer.py    From django-angularjs-blog with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def run(self):
        while True:
            try:
                addr = self.cq.get(block=True, timeout=3)
                self.proxy = addr
                if self.check_proxy():
                    print 'ip {0} is available'.format(addr)
                    self.save_addr(addr)
                self.cq.task_done()
            except Queue.Empty:
                break
            except Exception, e:
                print e 
Example #19
Source File: connectionpool.py    From jbox with MIT License 5 votes vote down vote up
def _get_conn(self, timeout=None):
        """
        Get a connection. Will return a pooled connection if one is available.

        If no connections are available and :prop:`.block` is ``False``, then a
        fresh connection is returned.

        :param timeout:
            Seconds to wait before giving up and raising
            :class:`urllib3.exceptions.EmptyPoolError` if the pool is empty and
            :prop:`.block` is ``True``.
        """
        conn = None
        try:
            conn = self.pool.get(block=self.block, timeout=timeout)

        except AttributeError:  # self.pool is None
            raise ClosedPoolError(self, "Pool is closed.")

        except Empty:
            if self.block:
                raise EmptyPoolError(self,
                                     "Pool reached maximum size and no more "
                                     "connections are allowed.")
            pass  # Oh well, we'll create a new connection then

        # If this is a persistent connection, check if it got disconnected
        if conn and is_connection_dropped(conn):
            log.info("Resetting dropped connection: %s" % self.host)
            conn.close()
            if getattr(conn, 'auto_open', 1) == 0:
                # This is a proxied connection that has been mutated by
                # httplib._tunnel() and cannot be reused (since it would
                # attempt to bypass the proxy)
                conn = None

        return conn or self._new_conn() 
Example #20
Source File: executor.py    From ffn with Apache License 2.0 5 votes vote down vote up
def _run_executor(self):
    """Main loop of the server thread which runs TF code."""
    self._curr_infeed = 0
    logging.info('Executor starting.')

    while self.active_clients or self.total_clients < self.expected_clients:
      self.counters['executor-clients'].Set(self.active_clients)

      with timer_counter(self.counters, 'executor-input'):
        ready = []
        while (len(ready) < min(self.active_clients, self.batch_size) or
               not self.active_clients):
          try:
            data = self.input_queue.get(timeout=5)
          except queue.Empty:
            continue
          if data == 'exit':
            logging.info('Executor shut down requested.')
            return
          elif isinstance(data, int):
            client_id = data
            if client_id >= 0:
              self.total_clients += 1
              self.active_clients += 1
              logging.info('client %d starting', client_id)
            else:
              logging.info('client %d terminating', -client_id - 1)
              self.active_clients -= 1
          else:
            client_id, seed, image, fetches = data
            l = len(ready)
            self.input_seed[l, ..., 0] = seed
            self.input_image[l, ..., 0] = image
            ready.append(client_id)

      if ready:
        self._schedule_batch(ready, fetches)

    logging.info('Executor terminating.') 
Example #21
Source File: request.py    From im with GNU General Public License v3.0 5 votes vote down vote up
def process_requests(self, max_requests, wait_time_for_element=0):
        """
        Procesa solicitudes de la cola, utilizando el metodo "process" de la clase
        Request.
        * Como mucho procesa "max_requests" solicitudes (si max_requests <= 0, procesa
          todas las solicitudes de la cola

        Devuelve el numero de solicitudes procesadas
        """

        # Al trabajar de esta forma, estamos secuencializando la ejecucion de las peticiones
        # sin embargo, al localizar esto aqui podriamos, potencialmente, considerar el tratar
        # las peticiones en threads. Sin embargo, eso podria aumentar considerablemente la
        # complejidad del sistema
        requests_processed = 0
        empty = False
        while (max_requests <= 0 or requests_processed < max_requests) and not empty:
            try:
                if wait_time_for_element > 0:
                    _, request = self.get(True, wait_time_for_element)
                else:
                    _, request = self.get(False)
                request.process()
                requests_processed = requests_processed + 1
            except Empty:
                empty = True
        return requests_processed 
Example #22
Source File: file2package.py    From binaryanalysis with Apache License 2.0 5 votes vote down vote up
def filename2package(unpackreports, scantempdir, topleveldir, processors, scanenv, batcursors, batcons, scandebug=False, unpacktempdir=None):
	processtasks = []
	for i in unpackreports:
		if not 'checksum' in unpackreports[i]:
			continue
		processtasks.append(i)

	if processors == None:
		processamount = 1
	else:
		processamount = processors
	## create a queue for tasks, with a few threads reading from the queue
	## and looking up results and putting them in a result queue
	query = "select distinct package, packageversion, source, distroversion from file where filename = %s"
	scanmanager = multiprocessing.Manager()
	scanqueue = multiprocessing.JoinableQueue(maxsize=0)
	reportqueue = scanmanager.Queue(maxsize=0)
	processpool = []

	map(lambda x: scanqueue.put(x), processtasks)
	minprocessamount = min(len(processtasks), processamount)
	res = []

	for i in range(0,minprocessamount):
		p = multiprocessing.Process(target=grabpackage, args=(scanqueue,reportqueue,batcursors[i],query))
		processpool.append(p)
		p.start()

	scanqueue.join()

	while True:
		try:
			val = reportqueue.get_nowait()
			res.append(val)
			reportqueue.task_done()
		except Queue.Empty, e:
			## Queue is empty
			break 
Example #23
Source File: dpooling.py    From python-mysql-pool with MIT License 5 votes vote down vote up
def _remove_connections(self):
        """Close all connections

        This method closes all connections. It returns the number
        of connections it closed.

        Used mostly for tests.

        Returns int.
        """
        with CONNECTION_POOL_LOCK:
            cnt = 0
            cnxq = self._cnx_queue
            while cnxq.qsize():
                try:
                    cnx = cnxq.get(block=False)
                    cnx.disconnect()
                    cnt += 1
                except queue.Empty:
                    return cnt
                except errors.PoolError:
                    raise
                except errors.Error:
                    # Any other error when closing means connection is closed
                    pass

            return cnt 
Example #24
Source File: dpooling.py    From python-mysql-pool with MIT License 5 votes vote down vote up
def get_connection(self):
        """Get a connection from the pool

        This method returns an PooledMySQLConnection instance which
        has a reference to the pool that created it, and the next available
        MySQL connection.

        When the MySQL connection is not connect, a reconnect is attempted.

        Raises PoolError on errors.

        Returns a PooledMySQLConnection instance.
        """
        with CONNECTION_POOL_LOCK:
            try:
                cnx = self._cnx_queue.get(block=False)
                self.dynamic_load()
            except queue.Empty:
                raise errors.PoolError(
                    "Failed getting connection; pool exhausted")

            # pylint: disable=W0201,W0212
            if not cnx.is_connected() \
                    or self._config_version != cnx._pool_config_version:
                cnx.config(**self._cnx_config)
                try:
                    cnx.reconnect()
                except errors.InterfaceError:
                    # Failed to reconnect, give connection back to pool
                    self._queue_connection(cnx)
                    raise
                cnx._pool_config_version = self._config_version
            # pylint: enable=W0201,W0212

            return PooledMySQLConnection(self, cnx) 
Example #25
Source File: connectionpool.py    From recruit with Apache License 2.0 5 votes vote down vote up
def _get_conn(self, timeout=None):
        """
        Get a connection. Will return a pooled connection if one is available.

        If no connections are available and :prop:`.block` is ``False``, then a
        fresh connection is returned.

        :param timeout:
            Seconds to wait before giving up and raising
            :class:`urllib3.exceptions.EmptyPoolError` if the pool is empty and
            :prop:`.block` is ``True``.
        """
        conn = None
        try:
            conn = self.pool.get(block=self.block, timeout=timeout)

        except AttributeError:  # self.pool is None
            raise ClosedPoolError(self, "Pool is closed.")

        except Empty:
            if self.block:
                raise EmptyPoolError(self,
                                     "Pool reached maximum size and no more "
                                     "connections are allowed.")
            pass  # Oh well, we'll create a new connection then

        # If this is a persistent connection, check if it got disconnected
        if conn and is_connection_dropped(conn):
            log.info("Resetting dropped connection: %s", self.host)
            conn.close()
            if getattr(conn, 'auto_open', 1) == 0:
                # This is a proxied connection that has been mutated by
                # httplib._tunnel() and cannot be reused (since it would
                # attempt to bypass the proxy)
                conn = None

        return conn or self._new_conn() 
Example #26
Source File: dataloader.py    From EMANet with GNU General Public License v3.0 5 votes vote down vote up
def _get_batch(self):
        if self.timeout > 0:
            try:
                return self.data_queue.get(timeout=self.timeout)
            except queue.Empty:
                raise RuntimeError('DataLoader timed out after {} seconds'.format(self.timeout))
        else:
            return self.data_queue.get() 
Example #27
Source File: bulkloader.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def PerformWork(self):
    """Performs the work of a ProgressTrackerThread."""
    while not self.exit_flag:
      try:
        item = self.progress_queue.get(block=True, timeout=1.0)
      except Queue.Empty:

        continue
      if item == _THREAD_SHOULD_EXIT:
        break

      if item.state == STATE_READ and item.progress_key is None:


        item.progress_key = self.db.StoreKeys(item.kind,
                                              item.key_start,
                                              item.key_end)
      else:



        assert item.progress_key is not None
        self.UpdateProgress(item)


      item.progress_event.set()

      self.progress_queue.task_done()

    self.db.ThreadComplete() 
Example #28
Source File: requeue.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def get(self, block=True, timeout=None):
    """Get an item from the requeue.

    Args:
      block: Whether to block if the requeue is empty.
      timeout: Maximum on how long to wait until the requeue is non-empty.

    Returns:
      An item from the requeue.

    Raises:
      Queue.Empty if the queue is empty and the timeout expires.
    """
    def GetAction():

      try:
        result = self.requeue.get(block=False)


        self.requeue.task_done()
      except Queue.Empty:

        result = self.queue.get(block=False)

      return result
    return self._DoWithTimeout(GetAction,
                               Queue.Empty,
                               self.put_cond,
                               self.get_cond,
                               self.lock,
                               timeout=timeout,
                               block=block) 
Example #29
Source File: connectionpool.py    From jbox with MIT License 5 votes vote down vote up
def _get_conn(self, timeout=None):
        """
        Get a connection. Will return a pooled connection if one is available.

        If no connections are available and :prop:`.block` is ``False``, then a
        fresh connection is returned.

        :param timeout:
            Seconds to wait before giving up and raising
            :class:`urllib3.exceptions.EmptyPoolError` if the pool is empty and
            :prop:`.block` is ``True``.
        """
        conn = None
        try:
            conn = self.pool.get(block=self.block, timeout=timeout)

        except AttributeError:  # self.pool is None
            raise ClosedPoolError(self, "Pool is closed.")

        except Empty:
            if self.block:
                raise EmptyPoolError(self,
                                     "Pool reached maximum size and no more "
                                     "connections are allowed.")
            pass  # Oh well, we'll create a new connection then

        # If this is a persistent connection, check if it got disconnected
        if conn and is_connection_dropped(conn):
            log.info("Resetting dropped connection: %s", self.host)
            conn.close()
            if getattr(conn, 'auto_open', 1) == 0:
                # This is a proxied connection that has been mutated by
                # httplib._tunnel() and cannot be reused (since it would
                # attempt to bypass the proxy)
                conn = None

        return conn or self._new_conn() 
Example #30
Source File: connectionpool.py    From recruit with Apache License 2.0 5 votes vote down vote up
def close(self):
        """
        Close all pooled connections and disable the pool.
        """
        # Disable access to the pool
        old_pool, self.pool = self.pool, None

        try:
            while True:
                conn = old_pool.get(block=False)
                if conn:
                    conn.close()

        except Empty:
            pass  # Done.