queue

Deletion in queue from front in Python

Deletion in queue from front in Python Question: I have written a program for implementaion of queue using Python. Insertion and display of the queue works fine. But I can’t delete element from front of queue. I have tried del keyword as well as pop method. In both the ways, the element is deleted from …

Total answers: 1

create threads within processes and share a queue between the threads

create threads within processes and share a queue between the threads Question: I am trying to create 3 threads within each of 2 processes and share a queue of the type multiprocessing.JoinableQueue among all threads. The worker_func function simply creates the threads while the thread_func function prints out the values it gets from the queue. …

Total answers: 1

How to avoid duplicate processing in python API server?

How to avoid duplicate processing in python API server? Question: Suppose a function detect_primes is expensive to call, and I would like to avoid repeated calls to it with duplicate parameters. What should I do? Using caching does not help because the function could be called concurrently in different requests. When both of the requests …

Total answers: 2

Python CancelledError with asyncio queue

Python CancelledError with asyncio queue Question: I use code from this answer, but get asyncio.exceptions.CancelledError when queue is empty. In real project, I add tasks to queue from consumers, that’s why I use while True statement I compress that code to make debug more easily: import asyncio import traceback async def consumer(queue: asyncio.Queue): try: while …

Total answers: 1

Is it possible to create a FIFO queue with pyTorch?

Is it possible to create a FIFO queue with pyTorch? Question: I need to create a fixed length Tensor in pyTorch that acts like a FIFO queue. I have this fuction to do it: def push_to_tensor(tensor, x): tensor[:-1] = tensor[1:] tensor[-1] = x return tensor For example, I have: tensor = Tensor([1,2,3,4]) >> tensor([ 1., …

Total answers: 3

Python – What is queue.task_done() used for?

Python – What is queue.task_done() used for? Question: I wrote a script that has multiple threads (created with threading.Thread) fetching URLs from a Queue using queue.get_nowait(), and then processing the HTML. I am new to multi-threaded programming, and am having trouble understanding the purpose of the queue.task_done() function. When the Queue is empty, it automatically …

Total answers: 4

Python multiprocessing.Queue vs multiprocessing.manager().Queue()

Python multiprocessing.Queue vs multiprocessing.manager().Queue() Question: I have a simple task like that: def worker(queue): while True: try: _ = queue.get_nowait() except Queue.Empty: break if __name__ == ‘__main__’: manager = multiprocessing.Manager() # queue = multiprocessing.Queue() queue = manager.Queue() for i in range(5): queue.put(i) processes = [] for i in range(2): proc = multiprocessing.Process(target=worker, args=(queue,)) processes.append(proc) proc.start() …

Total answers: 2

How would I go about using concurrent.futures and queues for a real-time scenario?

Use concurrent.futures with new tasks for a real-time scenario Question: It is fairly easy to do parallel work with Python 3’s concurrent.futures module as shown below. with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: future_to = {executor.submit(do_work, input, 60): input for input in dictionary} for future in concurrent.futures.as_completed(future_to): data = future.result() It is also very handy to insert and …

Total answers: 4

built-in max heap API in Python

built-in max heap API in Python Question: Default heapq is min queue implementation and wondering if there is an option for max queue? Thanks. I tried the solution using _heapify_max for max heap, but how to handle dynamically push/pop element? It seems _heapify_max could only be used during initialization time. import heapq def heapsort(iterable): h …

Total answers: 2