Python源码示例:queue.PriorityQueue()

示例1
def cull_it(c):
    global CULL_RATE
    c_temp = PriorityQueue()
    qsize = c.qsize()
    l = int(qsize - qsize * CULL_RATE)
    result_out('[i]\tPopulation size %d, cull rate %s, living specimens: %d' % (c.qsize(), str(CULL_RATE), l))
    result_out('[.]\tBeginning the cull of underperforming creatures...')
    for i in range(l):
        flag = 0
        while flag == 0:
            tmp = c.get()
            if tmp[1].genome != '':
                c_temp.put(tmp)
                flag = 1
    result_out('[+]\tCull done!')
    return c_temp 
示例2
def neighbor_search(merit_func, old_vars, global_best, idx, max_depth, show_progress=True):
    nonconvex_vars = get_noncvx_vars(merit_func)
    for var in nonconvex_vars:
        var.value = var.z.value
    best_so_far = [merit_func.value, {v.id:v.value for v in merit_func.variables()}]

    eval_queue = PriorityQueue()
    add_neighbors(eval_queue, merit_func, best_so_far[1], old_vars, 1)
    count = 0
    while not eval_queue.empty():
        merit, _, node_depth, _, sltn = eval_queue.get()
        count += 1
        if merit < best_so_far[0]:# and merit < global_best[0]:
            if idx == 0 and show_progress:
                print(merit, count)
            best_so_far[0] = merit
            best_so_far[1] = sltn
            if node_depth < max_depth:
                add_neighbors(eval_queue, merit_func, sltn, old_vars, node_depth+1)

    return best_so_far 
示例3
def dijkstra(self, from_vertex: int, to_vertex: int) -> None:
        vertices = [Vertex(i) for i in range(self._num_vertices)]
        vertices[from_vertex].distance_to_start = 0
        visited = [False] * self._num_vertices
        predecessor = [-1] * self._num_vertices
        q = PriorityQueue()
        q.put(vertices[from_vertex])
        visited[from_vertex] = True
        while not q.empty():
            min_vertex = q.get()
            if min_vertex.vertex_id == to_vertex:
                break
            for edge in self._adjacency[min_vertex.vertex_id]:
                next_vertex = vertices[edge.end_id]
                if min_vertex.distance_to_start + edge.weight < next_vertex.distance_to_start:
                    next_vertex.distance_to_start = min_vertex.distance_to_start + edge.weight
                    predecessor[next_vertex.vertex_id] = min_vertex.vertex_id
                    if not visited[next_vertex.vertex_id]:
                        q.put(next_vertex)
                        visited[next_vertex.vertex_id] = True
            
        path = lambda x: path(predecessor[x]) + [str(x)] if from_vertex != x else [str(from_vertex)]
        print("->".join(path(to_vertex))) 
示例4
def quantize(self):
        if self.h * self.w < self.maxColor:
            raise AttributeError("Image({0}x{1}) too small to be quantized".format(self.w, self.h))
        self.pixHisto = self.getPixHisto()

        orgVbox = self.createVbox(self.pixData)
        pOneQueue = PQueue(self.maxColor)
        pOneQueue.put((orgVbox.priority, orgVbox))
        popcolors = int(self.maxColor * self.fraction)

        pOneQueue = self.iterCut(popcolors, pOneQueue)

        boxQueue = PQueue(self.maxColor)
        while not pOneQueue.empty():
            vbox = pOneQueue.get()[1]
            vbox.priority *= vbox.vol
            boxQueue.put((vbox.priority, vbox))
        boxQueue = self.iterCut(self.maxColor - popcolors + 1, boxQueue, True)

        theme = []
        while not boxQueue.empty():
            theme.append(self.boxAvgColor(boxQueue.get()[1]))
        return theme 
示例5
def __init__(self, agent_name: str, comm: CommunicationLayer, delay: float = None):
        self._queue = PriorityQueue()
        self._local_agent = agent_name
        self.discovery = comm.discovery
        self._comm = comm
        self._comm.messaging = self
        self._delay = delay
        self.logger = logging.getLogger(f"infrastructure.communication.{agent_name}")

        # Keep track of failer messages to retry later
        self._failed = []

        # Containers for metrics on sent messages:
        self.count_ext_msg = defaultdict(lambda: 0)  # type: Dict[str, int]
        self.size_ext_msg = defaultdict(lambda: 0)  # type: Dict[str, int]
        self.last_msg_time = 0
        self.msg_queue_count = 0

        self._shutdown = False 
示例6
def huffman_hidden():  # builds the tree and returns root
    q = Queue.PriorityQueue()

    for key in freq:
        q.put((freq[key], key, Node(freq[key], key)))

    while q.qsize() != 1:
        a = q.get()
        b = q.get()
        obj = Node(a[0] + b[0], '\0')
        obj.left = a[2]
        obj.right = b[2]
        q.put((obj.freq, obj.data, obj))

    root = q.get()
    root = root[2]  # contains root object
    return root 
示例7
def test_process_synchronize_sources_msg_db_error(self, mock_process_message):
        """Test processing synchronize messages with database errors."""
        provider = Sources.objects.create(**self.aws_source)
        provider.save()
        future_mock = asyncio.Future()
        future_mock.set_result("test result")

        test_queue = queue.PriorityQueue()

        test_matrix = [
            {"test_value": {"operation": "update", "provider": provider}, "side_effect": InterfaceError},
            {"test_value": {"operation": "update", "provider": provider}, "side_effect": OperationalError},
        ]

        for i, test in enumerate(test_matrix):
            mock_process_message.side_effect = test.get("side_effect")
            with patch("sources.kafka_listener.connection.close") as close_mock:
                with patch.object(Config, "RETRY_SECONDS", 0):
                    process_synchronize_sources_msg((i, test["test_value"]), test_queue)
                    close_mock.assert_called()
        for i in range(2):
            priority, _ = test_queue.get_nowait()
            self.assertEqual(priority, i) 
示例8
def test_process_synchronize_sources_msg(self, mock_process_message):
        """Test processing synchronize messages."""
        provider = Sources(**self.aws_source)

        test_queue = queue.PriorityQueue()

        messages = [
            {"operation": "create", "provider": provider, "offset": provider.offset},
            {"operation": "update", "provider": provider},
        ]

        for msg in messages:
            with patch("sources.storage.clear_update_flag") as mock_clear_flag:
                process_synchronize_sources_msg((0, msg), test_queue)
                mock_clear_flag.assert_called()

        msg = {"operation": "destroy", "provider": provider}
        with patch("sources.storage.clear_update_flag") as mock_clear_flag:
            process_synchronize_sources_msg((0, msg), test_queue)
            mock_clear_flag.assert_not_called() 
示例9
def mergeKLists(self, lists) -> ListNode:
        # 使用优先队列(堆排序)存储每个list中的第一个节点,由优先队列返回优先级最高(值最低的元素)
        head_pre = p = ListNode(0)
        prior_queue = PriorityQueue()
        for list_node in lists:
            if list_node:
                prior_queue.put((list_node.val, list_node))
        while not prior_queue.empty():
            node_val, node = prior_queue.get()
            p.next = ListNode(node_val)
            p = p.next
            # 将当前list的头指向list中的下一个节点
            node = node.next
            if node:
                prior_queue.put((node.val, node))
        return head_pre.next 
示例10
def open_channel(self, worker_id, assignment_id):
        """
        Opens a channel for a worker on a given assignment, doesn't re-open if the
        channel is already open.
        """
        connection_id = '{}_{}'.format(worker_id, assignment_id)
        if connection_id in self.queues and self.run[connection_id]:
            shared_utils.print_and_log(
                logging.DEBUG, 'Channel ({}) already open'.format(connection_id)
            )
            return
        self.run[connection_id] = True
        self.queues[connection_id] = PriorityQueue()
        self.last_sent_heartbeat_time[connection_id] = 0
        self.pongs_without_heartbeat[connection_id] = 0
        self.last_received_heartbeat[connection_id] = None
        self.worker_assign_ids[connection_id] = (worker_id, assignment_id) 
示例11
def open_channel(self, worker_id, assignment_id):
        """
        Opens a channel for a worker on a given assignment, doesn't re-open if the
        channel is already open.
        """
        connection_id = '{}_{}'.format(worker_id, assignment_id)
        if connection_id in self.queues and self.run[connection_id]:
            shared_utils.print_and_log(
                logging.DEBUG, 'Channel ({}) already open'.format(connection_id)
            )
            return
        self.run[connection_id] = True
        self.queues[connection_id] = PriorityQueue()
        self.last_sent_heartbeat_time[connection_id] = 0
        self.pongs_without_heartbeat[connection_id] = 0
        self.last_received_heartbeat[connection_id] = None
        self.worker_assign_ids[connection_id] = (worker_id, assignment_id) 
示例12
def __init__(self, host, port = 5551):
		self._sock = socket.create_connection((host, port), None)
		self._lock = threading.RLock()
		self._scanners = {}
		self._scan_wizards = {}
		self._connection_channels = {}
		self._get_info_response_queue = queue.Queue()
		self._get_button_uuid_queue = queue.Queue()
		self._timers = queue.PriorityQueue()
		self._handle_event_thread_ident = None
		self._closed = False
		
		self.on_new_verified_button = lambda bd_addr: None
		self.on_no_space_for_new_connection = lambda max_concurrently_connected_buttons: None
		self.on_got_space_for_new_connection = lambda max_concurrently_connected_buttons: None
		self.on_bluetooth_controller_state_change = lambda state: None 
示例13
def __init__(self, n, adj, cost):
        # See description of these parameters in the starter for
        # friend_suggestion
        self.n = n
        self.INFINITY = n * maxlen
        self.adj = adj
        self.cost = cost
        self.bidistance = [[self.INFINITY] * n, [self.INFINITY] * n]
        self.visited = [False] * n
        self.visited = []
        self.q = queue.PriorityQueue()
        # Levels of nodes for node ordering heuristics
        self.level = [0] * n
        # Positions of nodes in the node ordering
        self.rank = [0] * n

        # Implement preprocessing here
        pass 
示例14
def __init__(self, n, adj, cost):
        # See description of these parameters in the starter for
        # friend_suggestion
        self.n = n
        self.INFINITY = n * maxlen
        self.adj = adj
        self.cost = cost
        self.bidistance = [[self.INFINITY] * n, [self.INFINITY] * n]
        self.visited = [False] * n
        self.visited = []
        self.q = queue.PriorityQueue()
        # Levels of nodes for node ordering heuristics
        self.level = [0] * n
        # Positions of nodes in the node ordering
        self.rank = [0] * n

        # Implement preprocessing here
        pass 
示例15
def __init__(self, n, adj, cost):
        # See description of these parameters in the starter for
        # friend_suggestion
        self.n = n
        self.INFINITY = n * maxlen
        self.adj = adj
        self.cost = cost
        self.bidistance = [[self.INFINITY] * n, [self.INFINITY] * n]
        self.visited = [False] * n
        self.visited = []
        self.q = queue.PriorityQueue()
        # Levels of nodes for node ordering heuristics
        self.level = [0] * n
        # Positions of nodes in the node ordering
        self.rank = [0] * n

        # Implement preprocessing here
        pass 
示例16
def __init__(self):
        self.todo_queue = queue.PriorityQueue()
        self.done_queues = {}
        self.done_tasks_time = dict()
        self.done_tasks_time_max = (60.0 * 20)
        self.broadcast_todo_queue = {}
        self.start_calc = {}
        self.out_tasks = {}
        self.proj_alive_time = {}
        self.proj_alive_time_max = (60 * 5)
        self.worker_alive_time = {}
        self.worker_alive_time_max = (60 * 30)
        self.pub_logger = logging.getLogger()
        self.pub_logger.setLevel(logging.WARN)
        if False:
            h = logging.handlers.RotatingFileHandler(filename='./server.log', mode='a', maxBytes=1000000, backupCount=10)
        else:
            h = logging.handlers.RotatingFileHandler(filename='./server.log', mode='a')
        f = logging.Formatter('%(asctime)s %(host)-16s %(job_id)-16s %(source_type)-8s %(levelname)-8s %(message)s')
        h.setFormatter(f)
        self.pub_logger.addHandler(h)
        self.logger = logging.LoggerAdapter(logging.getLogger(), {'host': os.environ.get('HOSTNAME', 'unknown'), 'job_id': os.environ.get('PBS_JOBID', 'N/A').split('.')[0], 'source_type': 'queue_server', })
        self.process = psutil.Process(os.getpid())
        _thread.start_new_thread(QueueServer.remove_dead_projects_daemon, (self,)) 
示例17
def dijkstra(dist, graph, src):
    que = PriorityQueue()
    que.put(edge(0, src))
    while not que.empty():
        p = que.get()
        v = p.to
        if dist[src][v] < p.cost:
            continue
        for i in range(len(graph[v])):
            if dist[src][graph[v][i].to] > dist[src][v] + graph[v][i].cost:
                dist[src][graph[v][i].to] = dist[src][v] + graph[v][i].cost
                que.put(edge(dist[src][graph[v][i].to], graph[v][i].to))


# @param{dist:距离矩阵,dims:降维的维度}
# return:降维后的矩阵 
示例18
def dijkstra(self, from_vertex: int, to_vertex: int) -> None:
        vertices = [Vertex(i) for i in range(self._num_vertices)]
        vertices[from_vertex].distance_to_start = 0
        visited = [False] * self._num_vertices
        predecessor = [-1] * self._num_vertices
        q = PriorityQueue()
        q.put(vertices[from_vertex])
        visited[from_vertex] = True
        while not q.empty():
            min_vertex = q.get()
            if min_vertex.vertex_id == to_vertex:
                break
            for edge in self._adjacency[min_vertex.vertex_id]:
                next_vertex = vertices[edge.end_id]
                if min_vertex.distance_to_start + edge.weight < next_vertex.distance_to_start:
                    next_vertex.distance_to_start = min_vertex.distance_to_start + edge.weight
                    predecessor[next_vertex.vertex_id] = min_vertex.vertex_id
                    if not visited[next_vertex.vertex_id]:
                        q.put(next_vertex)
                        visited[next_vertex.vertex_id] = True
            
        path = lambda x: path(predecessor[x]) + [str(x)] if from_vertex != x else [str(from_vertex)]
        print("->".join(path(to_vertex))) 
示例19
def _process_nonrealtime_stop(self, state):
        import supriya.patterns

        if not state["has_stopped"]:
            state["has_stopped"] = True
        self._debug("UNWINDING")
        assert state["event_queue"].qsize() == 1

        event_tuple = state["event_queue"].get()
        if event_tuple.iterator_index not in state["visited_iterators"]:
            self._debug("    DISCARDING, UNVISITED", event_tuple)
        elif not isinstance(event_tuple.event, supriya.patterns.CompositeEvent):
            self._debug("    DISCARDING, NON-COMPOSITE", event_tuple)
        elif not event_tuple.event.get("is_stop"):
            self._debug("    DISCARDING, NON-STOP", event_tuple)
        else:
            self._debug("    PRESERVING", event_tuple)
            state["event_queue"].put(event_tuple._replace(offset=0.0))

        iterator_queue = PriorityQueue()
        while not state["iterator_queue"].empty():
            iterator_tuple = state["iterator_queue"].get()
            iterator_tuple = iterator_tuple._replace(offset=0.0)
            iterator_queue.put(iterator_tuple)
        state["iterator_queue"] = iterator_queue 
示例20
def __init__(self):
        self._buffer = bytes()

        # Need to use composition instead of inheritance here
        self._observable = P2PObservable(self)

        self.last_rate_limit_update = 0
        self.rate_limit = config.user.peer_rate_limit
        self.in_counter = 0
        self.out_counter = 0

        self.bytes_sent = 0
        self.outgoing_queue = PriorityQueue(maxsize=config.user.p2p_q_size)

        self._connected_at = ntp.getTime()
        self._valid_message_count = 0

        self._public_port = 0 
示例21
def breed_it(ca):
    c_temp = PriorityQueue()
    result_out('[.]\tBreeding Next Generation...')
    while len(ca) > 0:
        if len(ca) == 1:
            cq = ca.pop(0)
            c_temp.put((cq.score, cq))
            return c_temp
        a = ca.pop(0)
        a1 = a.genome[0:len(a.genome) / 2]
        a2 = a.genome[len(a.genome):]
        # pull a random partner to mate with
        # it's a free society, after all
        b = ca.pop(random.randint(0, len(ca) - 1))
        b1 = b.genome[0:len(b.genome)]
        b2 = b.genome[len(b.genome):]
        c = Creature(0, a1 + b2)
        d = Creature(0, b1 + a2)
        e = Creature(0, mutate(a1 + b2))
        f = Creature(0, mutate(b1 + a2))
        a.modified = 0
        b.modified = 0
        c.modified = 1
        d.modified = 1
        e.modified = 1
        f.modified = 1
        c_temp.put((0, a))
        c_temp.put((0, b))
        c_temp.put((0, c))
        c_temp.put((0, d))
        c_temp.put((0, e))
        c_temp.put((0, f))
    result_out('[.]\tSuccess')
    return c_temp 
示例22
def __init__(self, size):
    PriorityQueue.__init__(self, size) 
示例23
def __cmp__(self, item):
    """ PriorityQueue uses this functino to sort the rewards
      Args:
        We sort the queue such that items with higher rewards are in the head of max-heap
    """
    return cmp(item.reward, self.reward) # bigger numbers have more priority 
示例24
def deserialize(str_data):
        newinf = InfrastructureInfo()
        dic = json.loads(str_data)
        vm_list = dic['vm_list']
        vm_master_id = dic['vm_master']
        dic['vm_master'] = None
        dic['vm_list'] = []
        if dic['auth']:
            dic['auth'] = Authentication.deserialize(dic['auth'])
        if dic['radl']:
            dic['radl'] = parse_radl(dic['radl'])
        else:
            dic['radl'] = RADL()
        if 'extra_info' in dic and dic['extra_info'] and "TOSCA" in dic['extra_info']:
            try:
                dic['extra_info']['TOSCA'] = Tosca.deserialize(dic['extra_info']['TOSCA'])
            except Exception:
                del dic['extra_info']['TOSCA']
                InfrastructureInfo.logger.exception("Error deserializing TOSCA document")
        newinf.__dict__.update(dic)
        newinf.cloud_connector = None
        # Set the ConfManager object and the lock to the data loaded
        newinf.cm = None
        newinf.ctxt_tasks = PriorityQueue()
        newinf.conf_threads = []
        for vm_data in vm_list:
            vm = VirtualMachine.deserialize(vm_data)
            vm.inf = newinf
            if vm.im_id == vm_master_id:
                newinf.vm_master = vm
            newinf.vm_list.append(vm)
        newinf.adding = False
        newinf.deleting = False
        return newinf 
示例25
def stop(self):
        """
        Stop all the Ctxt threads
        """
        # Stop the Ctxt thread if it is alive.
        if self.cm and self.cm.isAlive():
            self.cm.stop()

        # kill all the ctxt processes in the VMs
        for vm in self.get_vm_list():
            vm.kill_check_ctxt_process()

        # Create a new empty queue
        with self._lock:
            self.ctxt_tasks = PriorityQueue() 
示例26
def reset_ctxt_tasks(self):
        with self._lock:
            self.ctxt_tasks = PriorityQueue() 
示例27
def merge_k_lists(lists):
    dummy = ListNode(None)
    curr = dummy
    q = PriorityQueue()
    for node in lists:
        if node:
            q.put((node.val, node))
    while not q.empty():
        curr.next = q.get()[1]  # These two lines seem to
        curr = curr.next  # be equivalent to :-   curr = q.get()[1]
        if curr.next:
            q.put((curr.next.val, curr.next))
    return dummy.next 
示例28
def __init__(self):
        #self.queue = queue.PriorityQueue()
        self.queue :List[PItem] = []
        self.dirty = False 
示例29
def __init__(self, timeout=10):
        super().__init__(name='Dispatcher')
        self._timeout = timeout
        self._msg_type_handlers = {}
        self._in_queue = queue.PriorityQueue()
        self._send_message = {}
        self._send_last_message = {}
        self._message_information = {}
        self._condition = Condition()
        self._dispatch_timers = {}
        self._priority = {}
        self._preprocessors = {} 
示例30
def __init__(self, punctuate):
        self.pq = PriorityQueue()
        self.punctuate = punctuate