Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit f3574b7

Browse files
authored
Python package, bump version number (#4)
1 parent 7a5febc commit f3574b7

14 files changed

+126
-71
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ tools/
55
__pycache__/
66
*.py[cod]
77
.Python
8+
rpq.egg-info/
9+
build/
10+
dist/
811

912
# Mac
1013
.DS_Store

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,20 @@ Output is similar to [ZCOUNT](https://redis.io/commands/zcount)
103103

104104
The queues can be easily monitored with the Python script `src/queue_monitor.py`
105105

106-
To use the queue monitor, you need to ensure python is installed and use the following command to add the required packages:
107-
`pip install prettytable argparse redis json`
106+
To use the queue monitor, you need to ensure python is installed and use the following command:
107+
```
108+
# Installation
109+
pip3 install rpq
110+
111+
# Usage
112+
rpq_monitor
113+
```
108114

109115
### Usage example
110116

111117
```
112118
# Basic usage
113-
./src/queue_monitor.py -H [host] -p [port] (-a [auth] -n [dbnum])
119+
rpq_monitor -H [host] -p [port] (-a [auth] -n [dbnum])
114120
+-------------------+-------+-----------+----------+
115121
| Queue name | Total | Up to 100 | From 101 |
116122
+-------------------+-------+-----------+----------+
@@ -122,7 +128,7 @@ To use the queue monitor, you need to ensure python is installed and use the fol
122128
+-------------------+-------+-----------+----------+
123129
124130
# Specify your own groups
125-
./src/queue_monitor.py -H [host] -p [port] (-a [auth] -n [dbnum]) -s "[[0, 1000], [1001, 2000], [2001, 3000]]"
131+
rpq_monitor -H [host] -p [port] (-a [auth] -n [dbnum]) -s "[[0, 1000], [1001, 2000], [2001, 3000]]"
126132
+-------------------+-------+------------+----------------+----------------+
127133
| Queue name | Total | 0 to 1,000 | 1,001 to 2,000 | 2,001 to 3,000 |
128134
+-------------------+-------+------------+----------------+----------------+

clients/python/README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ Python client for redis-priority-queue.
55
## Requirements
66

77
- Python 2.7 & 3
8-
- Redis Python package (`pip install redis`)
8+
9+
## Installation
10+
11+
```
12+
pip3 install rpq
13+
```
914

1015
## Sample use
1116

@@ -14,18 +19,18 @@ Python client for redis-priority-queue.
1419

1520
# Packages
1621
import redis
17-
from lib.RpqQueue import RpqQueue
18-
from lib.RpqLua import RpqLua
22+
from rpq import RpqQueue
23+
from rpq import RpqLua
1924

2025
# Redis instance
21-
r = redis.StrictRedis(host='127.0.0.1', port=6379, db=0, password='')
26+
r = redis.StrictRedis(host = '127.0.0.1', port = 6379, db = 0, password = '')
2227

2328
# Load LUA Script
24-
RpqLua = RpqLua(r, '../../src/redis-priority-queue.lua');
29+
RpqLua = RpqLua.RpqLua(r);
2530
queue = RpqLua.register();
2631

2732
# RpqQueue instance
28-
RpqQueue = RpqQueue(queue)
33+
RpqQueue = RpqQueue.RpqQueue(queue)
2934

3035
# Set queue name
3136
RpqQueue.setqueueName('simple_queue')

clients/python/count.py renamed to clients/python/examples/count.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22

33
# Packages
44
import redis
5-
from lib.RpqQueue import RpqQueue
6-
from lib.RpqLua import RpqLua
7-
from utils.redis_vars import *
5+
from rpq import RpqQueue
6+
from rpq import RpqLua
87

98
# Redis instance
10-
# !!! set Redis credentials into utils/redis_vars.py
11-
r = redis.StrictRedis(host=rHost, port=rPort, db=rDbnum, password=rAuth)
9+
r = redis.StrictRedis(host = '127.0.0.1', port = 6379, db = 0, password = '')
1210

1311
# Load LUA Script
14-
RpqLua = RpqLua(r, '../../src/redis-priority-queue.lua');
12+
RpqLua = RpqLua.RpqLua(r);
1513
queue = RpqLua.register();
1614

1715
# RpqQueue instance
18-
RpqQueue = RpqQueue(queue);
16+
RpqQueue = RpqQueue.RpqQueue(queue);
1917

2018
# Set queue name
2119
RpqQueue.setqueueName('simple_queue')
@@ -29,4 +27,4 @@
2927
print (RpqQueue.count(0, 100))
3028

3129
print ('* count (priority > 100):')
32-
print (RpqQueue.count(101))
30+
print (RpqQueue.count(101))

clients/python/peek.py renamed to clients/python/examples/peek.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22

33
# Packages
44
import redis
5-
from lib.RpqQueue import RpqQueue
6-
from lib.RpqLua import RpqLua
7-
from utils.redis_vars import *
5+
from rpq import RpqQueue
6+
from rpq import RpqLua
87

98
def listItems(items):
109
'''Helper to print items'''
1110
for item in items:
1211
print (item);
1312

1413
# Redis instance
15-
# !!! set Redis credentials into utils/redis_vars.py
16-
r = redis.StrictRedis(host=rHost, port=rPort, db=rDbnum, password=rAuth)
14+
r = redis.StrictRedis(host = '127.0.0.1', port = 6379, db = 0, password = '')
1715

1816
# Load LUA Script
19-
RpqLua = RpqLua(r, '../../src/redis-priority-queue.lua');
17+
RpqLua = RpqLua.RpqLua(r);
2018
queue = RpqLua.register();
2119

2220
# RpqQueue instance
23-
RpqQueue = RpqQueue(queue);
21+
RpqQueue = RpqQueue.RpqQueue(queue);
2422

2523
# Set queue name
2624
RpqQueue.setqueueName('simple_queue')

clients/python/pop.py renamed to clients/python/examples/pop.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22

33
# Packages
44
import redis
5-
from lib.RpqQueue import RpqQueue
6-
from lib.RpqLua import RpqLua
7-
from utils.redis_vars import *
5+
from rpq import RpqQueue
6+
from rpq import RpqLua
87

98
def listItems(items):
109
'''Helper to print items'''
1110
for item in items:
1211
print (item);
1312

1413
# Redis instance
15-
# !!! set Redis credentials into utils/redis_vars.py
16-
r = redis.StrictRedis(host=rHost, port=rPort, db=rDbnum, password=rAuth)
14+
r = redis.StrictRedis(host = '127.0.0.1', port = 6379, db = 0, password = '')
1715

1816
# Load LUA Script
19-
RpqLua = RpqLua(r, '../../src/redis-priority-queue.lua');
17+
RpqLua = RpqLua.RpqLua(r);
2018
queue = RpqLua.register();
2119

2220
# RpqQueue instance
23-
RpqQueue = RpqQueue(queue);
21+
RpqQueue = RpqQueue.RpqQueue(queue);
2422

2523
# Set queue name
2624
RpqQueue.setqueueName('simple_queue')

clients/python/push.py renamed to clients/python/examples/push.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
# Packages
44
import redis, random
5-
from lib.RpqQueue import RpqQueue
6-
from lib.RpqLua import RpqLua
7-
from utils.redis_vars import *
5+
from rpq import RpqQueue
6+
from rpq import RpqLua
87

98
def generateRandom():
109
'''Helper: get a random integer'''
@@ -15,15 +14,14 @@ def getItem():
1514
return 'item_' + str(generateRandom());
1615

1716
# Redis instance
18-
# !!! set Redis credentials into utils/redis_vars.py
19-
r = redis.StrictRedis(host=rHost, port=rPort, db=rDbnum, password=rAuth)
17+
r = redis.StrictRedis(host = '127.0.0.1', port = 6379, db = 0, password = '')
2018

2119
# Load LUA Script
22-
RpqLua = RpqLua(r, '../../src/redis-priority-queue.lua');
20+
RpqLua = RpqLua.RpqLua(r);
2321
queue = RpqLua.register();
2422

2523
# RpqQueue instance
26-
RpqQueue = RpqQueue(queue);
24+
RpqQueue = RpqQueue.RpqQueue(queue);
2725

2826
# Set queue name
2927
RpqQueue.setqueueName('simple_queue')
@@ -38,4 +36,4 @@ def getItem():
3836
print ('* push (with priority):')
3937
item = getItem() # Item we will push to the queue
4038
print ('Pushing item "%s" to the queue' % (item))
41-
print (RpqQueue.push(item, 1000))
39+
print (RpqQueue.push(item, 1000))

clients/python/lib/RpqLua.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
11
#!/usr/bin/env python3
22

33
class RpqLua:
4-
def __init__(self, redisConnection, luaPath):
4+
def __init__(self, redisConnection):
55
'''Sets Redis connection, load LUA from path'''
6+
67
self.setRedisConnection(redisConnection);
7-
self.loadSource(luaPath);
8+
self.loadSource(self.getLuaPath());
9+
10+
def getLuaPath(self):
11+
"""
12+
Returns the LUA path in the filesystem
13+
"""
14+
15+
import rpq_src, pkg_resources
16+
17+
return pkg_resources.resource_filename('rpq_src', 'redis-priority-queue.lua');
818

919
def file_get_contents(self, filename):
1020
'''Get a file content'''
21+
1122
with open(filename) as f:
1223
return f.read()
1324

1425
def setRedisConnection(self, connection):
1526
'''Set a Redis connection'''
27+
1628
self.connection = connection;
1729

1830
def loadSource(self, path):
1931
'''Load LUA script source code from a file'''
32+
2033
self.source = self.file_get_contents(path);
2134

2235
def register(self):
2336
'''Load the script into Redis'''
37+
2438
self.queue = self.connection.register_script(self.source)
2539
return self.queue;

clients/python/utils/redis_vars.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

setup.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from setuptools import setup
2+
3+
setup (
4+
name = 'rpq',
5+
version = '1.0.4',
6+
description = 'Simple Redis work queue with added features (priorities, pop multiple items at once)',
7+
long_description = 'README.md',
8+
author = 'Gabriel Bordeaux',
9+
author_email = '[email protected]',
10+
url = 'https://github.com/gabfl/redis-priority-queue',
11+
license = 'MIT',
12+
packages = ['rpq', 'rpq_src'],
13+
package_dir = { 'rpq': 'clients/python/lib', 'rpq_src': 'src' },
14+
package_data={
15+
'rpq_src': ['*.lua'],
16+
},
17+
install_requires = ['redis', 'argparse', 'prettytable'], # external dependencies
18+
entry_points = {
19+
'console_scripts': [
20+
'rpq_monitor = rpq_src.queue_monitor:main',
21+
],
22+
},
23+
)
File renamed without changes.

src/queue_monitor.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
help="Sort groups", type=json.loads)
2323
args = parser.parse_args()
2424

25+
# Global vars
26+
t = None
27+
r = None
28+
2529
def setSortGroups(sortGroups = None):
2630
"""Return the sorting groups, either user defined or from the default list"""
2731
if sortGroups is None: # Default groups
@@ -32,10 +36,14 @@ def setSortGroups(sortGroups = None):
3236

3337
def getCount(queueName, min, max):
3438
"""Fetches set count from Redis"""
39+
40+
global r
41+
3542
return r.zcount(queueName, min, max);
3643

3744
def getColumnTitle(min, max):
3845
"""Human readable column titles"""
46+
3947
if str(min) == '-inf' and str(max) == '+inf':
4048
return 'Total';
4149
elif str(min) == '-inf':
@@ -49,38 +57,46 @@ def getColumnTitle(min, max):
4957

5058
def setColumnAlign(titles):
5159
"""Set PrettyTable column alignment"""
52-
global t;
60+
61+
global t
62+
5363
for i, title in enumerate(titles):
5464
if i == 0: # First column (title)
5565
t.align[title] = 'l'; # Left
5666
else: # Any other column
5767
t.align[title] = 'r'; # Right
5868

59-
# Redis connection
60-
r = redis.StrictRedis(host=args.host, port=args.port, db=args.dbnum, password=args.auth)
69+
def main():
70+
global t, r
71+
72+
# Redis connection
73+
r = redis.StrictRedis(host = args.host, port = args.port, db = args.dbnum, password = args.auth)
74+
75+
# Sort groups
76+
sortGroups = setSortGroups(args.sort_groups);
6177

62-
# Sort groups
63-
sortGroups = setSortGroups(args.sort_groups);
78+
# Column titles (queue name, then a column per sorting group)
79+
titles = ['Queue name'];
80+
titles.extend([getColumnTitle(sortGroup[0], sortGroup[1]) for sortGroup in sortGroups]);
6481

65-
# Column titles (queue name, then a column per sorting group)
66-
titles = ['Queue name'];
67-
titles.extend([getColumnTitle(sortGroup[0], sortGroup[1]) for sortGroup in sortGroups]);
82+
# Create table
83+
t = PrettyTable(titles);
84+
setColumnAlign(titles);
6885

69-
# Create table
70-
t = PrettyTable(titles);
71-
setColumnAlign(titles);
86+
# Get queues
87+
queueNames = r.smembers('rpq|names')
7288

73-
# Get queues
74-
queueNames = r.smembers('rpq|names')
89+
# Add a row par queue
90+
for queueName in sorted(queueNames):
91+
# Get row
92+
row = [queueName.decode("utf-8")]
93+
row.extend(['{0:,}'.format(getCount(queueName, sortGroup[0], sortGroup[1])) for sortGroup in sortGroups]);
7594

76-
# Add a row par queue
77-
for queueName in sorted(queueNames):
78-
# Get row
79-
row = [queueName.decode("utf-8")]
80-
row.extend(['{0:,}'.format(getCount(queueName, sortGroup[0], sortGroup[1])) for sortGroup in sortGroups]);
95+
# Add row
96+
t.add_row(row);
8197

82-
# Add row
83-
t.add_row(row);
98+
# Print table
99+
print (t);
84100

85-
# Print table
86-
print (t);
101+
if __name__ == '__main__':
102+
main()

src/redis-priority-queue.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- redis-priority-queue
22
-- Author: Gabriel Bordeaux (gabfl)
33
-- Github: https://github.com/gabfl/redis-priority-queue
4-
-- Version: 1.0.3
4+
-- Version: 1.0.4
55
-- (can only be used in 3.2+)
66

77
-- Get mandatory vars

0 commit comments

Comments
 (0)