-
Notifications
You must be signed in to change notification settings - Fork 1
/
qr.py
371 lines (315 loc) · 12 KB
/
qr.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""
QR | Redis-Based Data Structures in Python
"""
__author__ = 'Ted Nyman'
__version__ = '0.6.0'
__license__ = 'MIT'
import redis
import logging
try:
import json
except ImportError:
import simplejson as json
# This is a complete nod to hotqueue -- this is one of the
# things that they did right. Natively pickling and unpiclking
# objects is pretty useful.
try:
import cPickle as pickle
except ImportError:
import pickle
class NullHandler(logging.Handler):
"""A logging handler that discards all logging records"""
def emit(self, record):
pass
# Clients can add handlers if they are interested.
log = logging.getLogger('qr')
log.addHandler(NullHandler())
# A dictionary of connection pools, based on the parameters used
# when connecting. This is so we don't have an unwieldy number of
# connections
connectionPools = {}
def getRedis(**kwargs):
"""
Match up the provided kwargs with an existing connection pool.
In cases where you may want a lot of queues, the redis library will
by default open at least one connection for each. This uses redis'
connection pool mechanism to keep the number of open file descriptors
tractable.
"""
key = ':'.join((repr(key) + '=>' + repr(value)) for key, value in kwargs.items())
try:
return redis.Redis(connection_pool=connectionPools[key])
except KeyError:
cp = redis.ConnectionPool(**kwargs)
connectionPools[key] = cp
return redis.Redis(connection_pool=cp)
class worker(object):
def __init__(self, q, err=None, *args, **kwargs):
self.q = q
self.err = err
self.args = args
self.kwargs = kwargs
def __call__(self, f):
def wrapped():
while True:
# Blocking pop
next = self.q.pop(block=True)
if not next:
continue
try:
# Try to execute the user's callback.
f(next, *self.args, **self.kwargs)
except Exception as e:
try:
# Failing that, let's call the user's
# err-back, which we should keep from
# ever throwing an exception
self.err(e, *args, **kwargs)
except:
pass
return wrapped
class BaseQueue(object):
"""Base functionality common to queues"""
@staticmethod
def all(t, pattern, **kwargs):
r = getRedis(**kwargs)
return [t(k, **kwargs) for k in r.keys(pattern)]
def __init__(self, key, **kwargs):
self.serializer = pickle
self.redis = getRedis(**kwargs)
self.key = key
def __len__(self):
"""Return the length of the queue"""
return self.redis.llen(self.key)
def __getitem__(self, val):
"""Get a slice or a particular index."""
try:
return [self._unpack(i) for i in self.redis.lrange(self.key, val.start, val.stop - 1)]
except AttributeError:
return self._unpack(self.redis.lindex(self.key, val))
except Exception as e:
log.error('Get item failed ** %s' % repr(e))
return None
def _pack(self, val):
"""Prepares a message to go into Redis"""
return self.serializer.dumps(val, 1)
def _unpack(self, val):
"""Unpacks a message stored in Redis"""
try:
return self.serializer.loads(val)
except TypeError:
return None
def dump(self, fobj):
"""Destructively dump the contents of the queue into fp"""
next = self.redis.rpop(self.key)
while next:
fobj.write(next)
next = self.redis.rpop(self.key)
def load(self, fobj):
"""Load the contents of the provided fobj into the queue"""
try:
while True:
self.redis.lpush(self.key, self._pack(self.serializer.load(fobj)))
except:
return
def dumpfname(self, fname, truncate=False):
"""Destructively dump the contents of the queue into fname"""
if truncate:
with file(fname, 'w+') as f:
self.dump(f)
else:
with file(fname, 'a+') as f:
self.dump(f)
def loadfname(self, fname):
"""Load the contents of the contents of fname into the queue"""
with file(fname) as f:
self.load(f)
def extend(self, vals):
"""Extends the elements in the queue."""
with self.redis.pipeline(transaction=False) as pipe:
for val in vals:
pipe.lpush(self.key, self._pack(val))
pipe.execute()
def peek(self):
"""Look at the next item in the queue"""
return self[-1]
def elements(self):
"""Return all elements as a Python list"""
return [self._unpack(o) for o in self.redis.lrange(self.key, 0, -1)]
def elements_as_json(self):
"""Return all elements as JSON object"""
return json.dumps(self.elements)
def clear(self):
"""Removes all the elements in the queue"""
self.redis.delete(self.key)
class Deque(BaseQueue):
"""Implements a double-ended queue"""
@staticmethod
def all(pattern='*', **kwargs):
return BaseQueue.all(Deque, pattern, **kwargs)
def push_back(self, element):
"""Push an element to the back of the deque"""
self.redis.lpush(self.key, self._pack(element))
log.debug('Pushed ** %s ** for key ** %s **' % (element, self.key))
def push_front(self, element):
"""Push an element to the front of the deque"""
key = self.key
push_it = self.redis.rpush(key, self._pack(element))
log.debug('Pushed ** %s ** for key ** %s **' % (element, self.key))
def pop_front(self):
"""Pop an element from the front of the deque"""
popped = self.redis.rpop(self.key)
log.debug('Popped ** %s ** from key ** %s **' % (popped, self.key))
return self._unpack(popped )
def pop_back(self):
"""Pop an element from the back of the deque"""
popped = self.redis.lpop(self.key)
log.debug('Popped ** %s ** from key ** %s **' % (popped, self.key))
return self._unpack(popped)
class Queue(BaseQueue):
"""Implements a FIFO queue"""
@staticmethod
def all(pattern='*', **kwargs):
return BaseQueue.all(Queue, pattern, **kwargs)
def push(self, element):
"""Push an element"""
self.redis.lpush(self.key, self._pack(element))
log.debug('Pushed ** %s ** for key ** %s **' % (element, self.key))
def pop(self, block=False):
"""Pop an element"""
if not block:
popped = self.redis.rpop(self.key)
else:
queue, popped = self.redis.brpop(self.key)
log.debug('Popped ** %s ** from key ** %s **' % (popped, self.key))
return self._unpack(popped)
class PriorityQueue(BaseQueue):
"""A priority queue"""
def __len__(self):
"""Return the length of the queue"""
return self.redis.zcard(self.key)
def __getitem__(self, val):
"""Get a slice or a particular index."""
try:
return [self._unpack(i) for i in self.redis.zrange(self.key, val.start, val.stop - 1)]
except AttributeError:
val = self.redis.zrange(self.key, val, val)
if val:
return self._unpack(val[0])
return None
except Exception as e:
log.error('Get item failed ** %s' % repr(e))
return None
def dump(self, fobj):
"""Destructively dump the contents of the queue into fp"""
next = self.pop()
while next:
self.serializer.dump(next[0], fobj)
next = self.pop()
def load(self, fobj):
"""Load the contents of the provided fobj into the queue"""
try:
while True:
value, score = self.serializer.load(fobj)
self.redis.zadd(self.key, value, score)
except Exception as e:
return
def dumpfname(self, fname, truncate=False):
"""Destructively dump the contents of the queue into fname"""
if truncate:
with file(fname, 'w+') as f:
self.dump(f)
else:
with file(fname, 'a+') as f:
self.dump(f)
def loadfname(self, fname):
"""Load the contents of the contents of fname into the queue"""
with file(fname) as f:
self.load(f)
def extend(self, vals):
"""Extends the elements in the queue."""
with self.redis.pipeline(transaction=False) as pipe:
for val, score in vals:
pipe.zadd(self.key, self._pack(val), score)
return pipe.execute()
def peek(self, withscores=False):
"""Look at the next item in the queue"""
val = self.redis.zrange(self.key, 0, 0, withscores=True)
if val:
value, score = val[0]
value = self._unpack(value)
if withscores:
return (value, score)
return value
elif withscores:
return (None, 0.0)
return None
def elements(self):
"""Return all elements as a Python list"""
return [self._unpack(o) for o in self.redis.zrange(self.key, 0, -1)]
def pop(self, withscores=False):
"""Get the element with the lowest score, and pop it off"""
with self.redis.pipeline() as pipe:
o = pipe.zrange(self.key, 0, 0, withscores=True)
o = pipe.zremrangebyrank(self.key, 0, 0)
results, count = pipe.execute()
if results:
value, score = results[0]
value = self._unpack(value)
if withscores:
return (value, score)
return value
elif withscores:
return (None, 0.0)
return None
def push(self, value, score):
'''Add an element with a given score'''
return self.redis.zadd(self.key, self._pack(value), score)
class CappedCollection(BaseQueue):
"""
Implements a capped collection (the collection never
gets larger than the specified size).
"""
@staticmethod
def all(pattern='*', **kwargs):
return BaseQueue.all(CappedCollection, pattern, **kwargs)
def __init__(self, key, size, **kwargs):
BaseQueue.__init__(self, key, **kwargs)
self.size = size
def push(self, element):
size = self.size
with self.redis.pipeline() as pipe:
# ltrim is zero-indexed
pipe = pipe.lpush(self.key, self._pack(element)).ltrim(self.key, 0, size-1)
pipe.execute()
def extend(self, vals):
"""Extends the elements in the queue."""
with self.redis.pipeline() as pipe:
for val in vals:
pipe.lpush(self.key, self._pack(val))
pipe.ltrim(self.key, 0, self.size-1)
pipe.execute()
def pop(self, block=False):
if not block:
popped = self.redis.rpop(self.key)
else:
queue, popped = self.redis.brpop(self.key)
log.debug('Popped ** %s ** from key ** %s **' % (popped, self.key))
return self._unpack(popped)
class Stack(BaseQueue):
"""Implements a LIFO stack"""
@staticmethod
def all(pattern='*', **kwargs):
return BaseQueue.all(Stack, pattern, **kwargs)
def push(self, element):
"""Push an element"""
self.redis.lpush(self.key, self._pack(element))
log.debug('Pushed ** %s ** for key ** %s **' % (element, self.key))
def pop(self, block=False):
"""Pop an element"""
if not block:
popped = self.redis.lpop(self.key)
else:
queue, popped = self.redis.blpop(self.key)
log.debug('Popped ** %s ** from key ** %s **' % (popped, self.key))
return self._unpack(popped)