Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove an item from the middle of a Queue?

Is it possible to remove an item and have the queue for multithreading reorder itself in Python?

I have users queued up in a Queue, but when it's time to process the user and if the user disconnects, that will raise an issue.

Is there a way to do this?

Thanks

like image 721
Pwnna Avatar asked Nov 18 '11 18:11

Pwnna


1 Answers

Queue objects do not provide random access. However, you can achieve the same goal by retaining references to the objects in the queue somewhere else and marking them stale. When the object is read from the queue, the consumer can check to see if the object is stale, and if it is, throw it away and read again.

You may also find value in the PriorityQueue class, where you put (priority, message) instead of just message onto the queue, and the data with the lowest priority value is always the next one read from the queue. This by itself won't solve your issue, but it may give you a way to prioritize the oldest request or something like that.

like image 141
wberry Avatar answered Oct 13 '22 17:10

wberry