-
Notifications
You must be signed in to change notification settings - Fork 0
/
ydl_queue.py
82 lines (67 loc) · 1.93 KB
/
ydl_queue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'''
I need a threadsafe queue I can take snapshots of the contents. The normal
python Queue implentations don't support that and I don't want to play games
with emptying and refilling them to see the contents.
'''
import copy
import threading
class YdlQueue(object):
'''
A simple queue class with the extension of adding 'peek_all' which gives us
a copy of the current state of the queue
'''
def __init__(self, max_size=None):
'''
basic constructor, making an internal queue and a lock
'''
#read/write would be good here. None in stdlib?!
self.lock = threading.Lock()
self.queue = []
self.max_size = max_size
def put(self, item):
'''
put item in the queue (in back). returns true if it worked
'''
with self.lock:
if self.max_size is not None and len(self.queue) > self.max_size:
return False
self.queue.append(item)
return True
def get(self):
'''
get item from the queue (front)
'''
ret = None
with self.lock:
if len(self.queue):
ret = self.queue[0]
self.queue = self.queue[1:]
return ret
def peek_all(self):
'''
get a copy of the queue
'''
ret = []
with self.lock:
ret = copy.copy(self.queue)
return ret
def clear(self):
'''
remove everything from the queue
'''
with self.lock:
self.queue = []
def remove(self, item):
'''
like remove from a list
'''
with self.lock:
if item in self.queue:
self.queue.remove(item)
def drop_lru(self, num):
'''
drop the least recently used keeping num elements
'''
with self.lock:
while len(self.queue) > num:
self.queue = self.queue[1:]