dsmq 0.4.0__py3-none-any.whl → 0.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dsmq/dsmq.py
CHANGED
@@ -10,13 +10,12 @@ DEFAULT_PORT = 30008
|
|
10
10
|
|
11
11
|
N_RETRIES = 5
|
12
12
|
FIRST_RETRY = 0.01 # seconds
|
13
|
+
TIME_TO_LIVE = 600.0 # seconds
|
13
14
|
|
14
15
|
|
15
16
|
def start_server(host=DEFAULT_HOST, port=DEFAULT_PORT):
|
16
17
|
"""
|
17
18
|
For best results, start this running in its own process and walk away.
|
18
|
-
|
19
|
-
|
20
19
|
"""
|
21
20
|
sqlite_conn = sqlite3.connect("file:mem1?mode=memory&cache=shared")
|
22
21
|
cursor = sqlite_conn.cursor()
|
@@ -79,8 +78,9 @@ def _handle_client_connection(socket_conn):
|
|
79
78
|
sqlite_conn = sqlite3.connect("file:mem1?mode=memory&cache=shared")
|
80
79
|
cursor = sqlite_conn.cursor()
|
81
80
|
|
82
|
-
|
83
|
-
|
81
|
+
client_creation_time = time.time()
|
82
|
+
last_read_times = {}
|
83
|
+
time_of_last_purge = time.time()
|
84
84
|
|
85
85
|
while True:
|
86
86
|
data = socket_conn.recv(1024)
|
@@ -90,7 +90,6 @@ def _handle_client_connection(socket_conn):
|
|
90
90
|
|
91
91
|
msg_str = data.decode("utf-8")
|
92
92
|
try:
|
93
|
-
# print("dsmq received ", msg_str)
|
94
93
|
msg = json.loads(msg_str)
|
95
94
|
except json.decoder.JSONDecodeError:
|
96
95
|
print("Message must be json-friendly")
|
@@ -123,10 +122,10 @@ VALUES (:timestamp, :topic, :message)
|
|
123
122
|
|
124
123
|
elif msg["action"] == "get":
|
125
124
|
try:
|
126
|
-
last_read_time =
|
125
|
+
last_read_time = last_read_times[topic]
|
127
126
|
except KeyError:
|
128
|
-
|
129
|
-
last_read_time =
|
127
|
+
last_read_times[topic] = client_creation_time
|
128
|
+
last_read_time = last_read_times[topic]
|
130
129
|
msg["last_read_time"] = last_read_time
|
131
130
|
|
132
131
|
# This block allows for multiple retries if the database
|
@@ -159,7 +158,7 @@ AND timestamp = a.min_time
|
|
159
158
|
result = cursor.fetchall()[0]
|
160
159
|
message = result[0]
|
161
160
|
timestamp = result[1]
|
162
|
-
|
161
|
+
last_read_times[topic] = timestamp
|
163
162
|
except IndexError:
|
164
163
|
# Handle the case where no results are returned
|
165
164
|
message = ""
|
@@ -169,6 +168,20 @@ AND timestamp = a.min_time
|
|
169
168
|
else:
|
170
169
|
print("Action must either be 'put' or 'get'")
|
171
170
|
|
171
|
+
# Periodically clean out messages from the queue that are
|
172
|
+
# past their sell buy date.
|
173
|
+
# This operation is pretty fast. I clock it at 12 us on my machine.
|
174
|
+
if time.time() - time_of_last_purge > TIME_TO_LIVE:
|
175
|
+
cursor.execute(
|
176
|
+
"""
|
177
|
+
DELETE FROM messages
|
178
|
+
WHERE timestamp < :time_threshold
|
179
|
+
""",
|
180
|
+
{"time_threshold": time_of_last_purge}
|
181
|
+
)
|
182
|
+
sqlite_conn.commit()
|
183
|
+
time_of_last_purge = time.time()
|
184
|
+
|
172
185
|
sqlite_conn.close()
|
173
186
|
|
174
187
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.3
|
2
2
|
Name: dsmq
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.6.0
|
4
4
|
Summary: A dead simple message queue
|
5
5
|
Requires-Python: >=3.10
|
6
6
|
Description-Content-Type: text/markdown
|
@@ -97,6 +97,8 @@ a queue before it connected.
|
|
97
97
|
- A client will get the oldest message available on a requested topic.
|
98
98
|
Queues are first-in-first-out.
|
99
99
|
|
100
|
+
- Messages older than 600 seconds will be deleted from the queue.
|
101
|
+
|
100
102
|
- Put and get operations are fairly quick--less than 100 $`\mu`$s of processing
|
101
103
|
time plus any network latency--so it can comfortably handle requests at rates of
|
102
104
|
hundreds of times per second. But if you have several clients reading and writing
|
@@ -127,7 +129,7 @@ Connects to an existing message queue server.
|
|
127
129
|
## `DSMQClientSideConnection` class
|
128
130
|
|
129
131
|
This is a convenience wrapper, to make the `get()` and `put()` functions
|
130
|
-
easy to write and remember.
|
132
|
+
easy to write and remember. It's under the hood only, not meant to be called directly.
|
131
133
|
|
132
134
|
### `put(topic, msg)`
|
133
135
|
|
@@ -143,4 +145,4 @@ connected to the server.
|
|
143
145
|
- `topic` (str)
|
144
146
|
- returns str, the content of the message. If there was no eligble message
|
145
147
|
in the topic, or the topic doesn't yet exist,
|
146
|
-
returns ""
|
148
|
+
returns `""`.
|
@@ -1,10 +1,10 @@
|
|
1
1
|
dsmq/__init__.py,sha256=YCgbnQAk8YbtHRyMcU0v2O7RdRhPhlT-vS_q40a7Q6g,50
|
2
2
|
dsmq/demo_linux.py,sha256=7yLglGmirDLuuyMxppYSK-dfx2Fg2Q0dIWB4cl2yV1c,622
|
3
|
-
dsmq/dsmq.py,sha256=
|
3
|
+
dsmq/dsmq.py,sha256=zg69Tf3x9z26a9NrEXTWgr78kcNKVszmLq0INv3IHuM,6132
|
4
4
|
dsmq/example_get_client.py,sha256=chFfB2949PBENmgdUc3ASrATq1m4wvHGBzEnOC-o_Xs,296
|
5
5
|
dsmq/example_put_client.py,sha256=mUKCRhmUieMZEpHLFWFvzeKB6IR7A8l4tWN8TvPzdKU,348
|
6
6
|
dsmq/example_server.py,sha256=kkXOPaaTzVxf9_iIM76zU9pZhkPna_1vcGWkPrhCjus,61
|
7
|
-
dsmq-0.
|
8
|
-
dsmq-0.
|
9
|
-
dsmq-0.
|
10
|
-
dsmq-0.
|
7
|
+
dsmq-0.6.0.dist-info/METADATA,sha256=0mL4N2uHDGtlnH_fqrdNLdpS-flp6x1O1g-uLGzF4b4,4006
|
8
|
+
dsmq-0.6.0.dist-info/WHEEL,sha256=3U_NnUcV_1B1kPkYaPzN-irRckL5VW_lytn0ytO_kRY,87
|
9
|
+
dsmq-0.6.0.dist-info/licenses/LICENSE,sha256=3Yu1mAp5VsKmnDtzkiOY7BdmrLeNwwZ3t6iWaLnlL0Y,1071
|
10
|
+
dsmq-0.6.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|