Python queue.PriorityQueue() Examples
The following are 30
code examples of queue.PriorityQueue().
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: Ppar.py From supriya with MIT License | 6 votes |
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
Example #2
Source File: queue_server.py From aitom with GNU General Public License v3.0 | 6 votes |
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,))
Example #3
Source File: test_kafka_listener.py From koku with GNU Affero General Public License v3.0 | 6 votes |
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()
Example #4
Source File: test_kafka_listener.py From koku with GNU Affero General Public License v3.0 | 6 votes |
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)
Example #5
Source File: shortest_path.py From wangzheng0822-algo with Apache License 2.0 | 6 votes |
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)))
Example #6
Source File: tree-huffman-decoding.py From hackerrank with MIT License | 6 votes |
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
Example #7
Source File: socket_manager.py From ParlAI with MIT License | 6 votes |
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)
Example #8
Source File: dist_preprocess_large.py From coursera-dsa with MIT License | 6 votes |
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
Example #9
Source File: ISOMAP.py From curriculum with GNU General Public License v3.0 | 6 votes |
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:降维后的矩阵
Example #10
Source File: communication.py From pyDcop with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #11
Source File: MMCQ.py From ImageColorTheme with MIT License | 6 votes |
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
Example #12
Source File: dist_preprocess_small.py From coursera-dsa with MIT License | 6 votes |
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
Example #13
Source File: 23_merge-k-sorted-lists.py From algorithm with Apache License 2.0 | 6 votes |
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
Example #14
Source File: jf-web.py From jawfish with MIT License | 6 votes |
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
Example #15
Source File: travelling_salesman_problem.py From coursera-dsa with MIT License | 6 votes |
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
Example #16
Source File: shortest_path.py From algo with Apache License 2.0 | 6 votes |
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)))
Example #17
Source File: admm_problem.py From ncvx with GNU General Public License v3.0 | 6 votes |
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
Example #18
Source File: p2pprotocol.py From QRL with MIT License | 6 votes |
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
Example #19
Source File: socket_manager.py From ParlAI with MIT License | 6 votes |
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)
Example #20
Source File: fliclib.py From platypush with MIT License | 6 votes |
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
Example #21
Source File: merge_sorted_k_lists.py From algorithms with MIT License | 5 votes |
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
Example #22
Source File: tweetlord.py From tweetlord with GNU General Public License v3.0 | 5 votes |
def _build_queue(self, section, method): queue = PriorityQueue(len(self._creds)) for i in range(len(self._creds)): queue.put(( -(self._app_limits[i]['resources'][section][method]['remaining'] + self._user_limits[i]['resources'][section][method]['remaining']), max(self._app_limits[i]['resources'][section][method]['reset'], self._user_limits[i]['resources'][section][method]['reset']), self._creds[i] )) return queue # ---------------------------------------------------------- # ------------------------- Utils -------------------------- # ----------------------------------------------------------
Example #23
Source File: queue_server.py From aitom with GNU General Public License v3.0 | 5 votes |
def get_broadcast_task(self, worker_id): self.worker_alive_time[worker_id] = time.time() if (worker_id not in self.broadcast_todo_queue): self.broadcast_todo_queue[worker_id] = queue.PriorityQueue() return None try: (priority, task) = self.broadcast_todo_queue[worker_id].get_nowait() self.out_tasks[task.task_id] = task self.start_calc[task.task_id] = time.time() return task except queue.Empty: pass return None
Example #24
Source File: taxi_sim0.py From notebooks with MIT License | 5 votes |
def __init__(self, procs_map): self.events = queue.PriorityQueue() self.procs = dict(procs_map)
Example #25
Source File: TaskScheduler.py From dl_coursera with MIT License | 5 votes |
def _init(self): self._q = queue.PriorityQueue() self._threads = [] self._n_worker = 0 self._n_worker_lock = threading.Lock() self._shutdown_event = threading.Event() self._failures = [] self._failures_lock = threading.Lock() self._threading_local = threading.local() self._threading_local.task = None
Example #26
Source File: dist_preprocess_large.py From coursera-dsa with MIT License | 5 votes |
def query(self, s, t): q = [queue.PriorityQueue(), queue.PriorityQueue()] estimate = self.INFINITY self.visit(0, s, 0) self.visit(1, t, 0) # Implement the rest of the algorithm yourself return -1 if estimate == self.INFINITY else estimate
Example #27
Source File: dist_preprocess_small.py From coursera-dsa with MIT License | 5 votes |
def query(self, s, t): q = [queue.PriorityQueue(), queue.PriorityQueue()] estimate = self.INFINITY self.visit(0, s, 0) self.visit(1, t, 0) # Implement the rest of the algorithm yourself return -1 if estimate == self.INFINITY else estimate
Example #28
Source File: travelling_salesman_problem.py From coursera-dsa with MIT License | 5 votes |
def query(self, s, t): q = [queue.PriorityQueue(), queue.PriorityQueue()] estimate = self.INFINITY self.visit(0, s, 0) self.visit(1, t, 0) # Implement the rest of the algorithm yourself return -1 if estimate == self.INFINITY else estimate
Example #29
Source File: dist_with_coords.py From coursera-dsa with MIT License | 5 votes |
def query(self, s, t): self.clear() q = queue.PriorityQueue() # Implement the rest of the algorithm yourself return -1
Example #30
Source File: socket_manager.py From neural_chat with MIT License | 5 votes |
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.worker_assign_ids[connection_id] = (worker_id, assignment_id)