dsmq 1.0.0__py3-none-any.whl → 1.1.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/client.py CHANGED
@@ -1,9 +1,13 @@
1
1
  import json
2
+ import time
2
3
  from websockets.sync.client import connect as ws_connect
3
4
 
4
5
  _default_host = "127.0.0.1"
5
6
  _default_port = 30008
6
7
 
8
+ _n_retries = 10
9
+ _initial_retry = 0.01 # seconds
10
+
7
11
 
8
12
  def connect(host=_default_host, port=_default_port):
9
13
  return DSMQClientSideConnection(host, port)
@@ -12,16 +16,50 @@ def connect(host=_default_host, port=_default_port):
12
16
  class DSMQClientSideConnection:
13
17
  def __init__(self, host, port):
14
18
  self.uri = f"ws://{host}:{port}"
15
- self.websocket = ws_connect(self.uri)
19
+ print(f"Connecting to dsmq server at {self.uri}")
20
+ for i_retry in range(_n_retries):
21
+ try:
22
+ self.websocket = ws_connect(self.uri)
23
+ break
24
+ except ConnectionRefusedError:
25
+ self.websocket = None
26
+ # Exponential backoff
27
+ # Wait twice as long each time before trying again.
28
+ time.sleep(_initial_retry * 2**i_retry)
29
+ print(" ...trying again")
30
+
31
+ if self.websocket is None:
32
+ raise ConnectionRefusedError("Could not connect to dsmq server.")
33
+
34
+ self.time_of_last_request = time.time()
16
35
 
17
36
  def get(self, topic):
18
37
  msg = {"action": "get", "topic": topic}
19
38
  self.websocket.send(json.dumps(msg))
20
-
21
39
  msg_text = self.websocket.recv()
22
40
  msg = json.loads(msg_text)
23
41
  return msg["message"]
24
42
 
43
+ def get_wait(self, topic):
44
+ """
45
+ A variant of `get()` that retries a few times until it gets
46
+ a non-empty message. Adjust `_n_tries` and `_initial_retry`
47
+ to change how persistent it will be.
48
+ """
49
+ for i_retry in range(_n_retries):
50
+ message = self.get(topic)
51
+ if message != "":
52
+ return message
53
+ time.sleep(_initial_retry * 2**i_retry)
54
+ return message
55
+
25
56
  def put(self, topic, msg_body):
26
57
  msg_dict = {"action": "put", "topic": topic, "message": msg_body}
27
58
  self.websocket.send(json.dumps(msg_dict))
59
+
60
+ def shutdown_server(self):
61
+ msg_dict = {"action": "shutdown", "topic": ""}
62
+ self.websocket.send(json.dumps(msg_dict))
63
+
64
+ def close(self):
65
+ self.websocket.close()
@@ -1,11 +1,8 @@
1
- # Heads up: This script only works on Linux because of use of "fork" method.
2
1
  import multiprocessing as mp
3
2
  from dsmq.server import serve
4
3
  import dsmq.example_get_client
5
4
  import dsmq.example_put_client
6
5
 
7
- mp.set_start_method("fork")
8
-
9
6
  HOST = "127.0.0.1"
10
7
  PORT = 25252
11
8
 
dsmq/server.py CHANGED
@@ -1,6 +1,8 @@
1
1
  import json
2
+ import os
2
3
  import sqlite3
3
4
  import sys
5
+ from threading import Thread
4
6
  import time
5
7
  from websockets.sync.server import serve as ws_serve
6
8
 
@@ -8,30 +10,57 @@ _default_host = "127.0.0.1"
8
10
  _default_port = 30008
9
11
  _n_retries = 5
10
12
  _first_retry = 0.01 # seconds
13
+ _pause = 0.01 # seconds
11
14
  _time_to_live = 600.0 # seconds
12
15
 
16
+ _db_name = "file::memory:?cache=shared"
17
+
18
+ # Make this global so it's easy to share
19
+ dsmq_server = None
20
+
13
21
 
14
22
  def serve(host=_default_host, port=_default_port):
15
23
  """
16
24
  For best results, start this running in its own process and walk away.
17
25
  """
18
- sqlite_conn = sqlite3.connect("file:mem1?mode=memory&cache=shared")
26
+ # Cleanup temp files.
27
+ # Under some condition
28
+ # (which I haven't yet been able to pin down)
29
+ # a file is generated with the db name.
30
+ # If it is not removed, it gets
31
+ # treated as a SQLite db on disk,
32
+ # which dramatically slows it down,
33
+ # especially the way it's used here for
34
+ # rapid-fire one-item reads and writes.
35
+ filenames = os.listdir()
36
+ for filename in filenames:
37
+ if filename[: len(_db_name)] == _db_name:
38
+ os.remove(filename)
39
+
40
+ sqlite_conn = sqlite3.connect(_db_name)
19
41
  cursor = sqlite_conn.cursor()
20
42
  cursor.execute("""
21
43
  CREATE TABLE IF NOT EXISTS messages (timestamp DOUBLE, topic TEXT, message TEXT)
22
44
  """)
23
45
 
24
- with ws_serve(request_handler, host, port) as server:
25
- server.serve_forever()
26
- print()
27
- print(f"Server started at {host} on port {port}.")
28
- print("Waiting for clients...")
46
+ # Making this global in scope is a way to make it available
47
+ # to the shutdown operation. It's an awkward construction,
48
+ # and a method of last resort. (If you stumble across this and
49
+ # figure out something more elegant, please submit a PR!
50
+ # or send it to me at brohrer@gmail.com,
51
+ global dsmq_server
52
+
53
+ dsmq_server = ws_serve(request_handler, host, port)
54
+ dsmq_server.serve_forever()
55
+ print()
56
+ print(f"Server started at {host} on port {port}.")
57
+ print("Waiting for clients...")
29
58
 
30
59
  sqlite_conn.close()
31
60
 
32
61
 
33
62
  def request_handler(websocket):
34
- sqlite_conn = sqlite3.connect("file:mem1?mode=memory&cache=shared")
63
+ sqlite_conn = sqlite3.connect(_db_name)
35
64
  cursor = sqlite_conn.cursor()
36
65
 
37
66
  client_creation_time = time.time()
@@ -85,7 +114,7 @@ FROM messages,
85
114
  SELECT MIN(timestamp) AS min_time
86
115
  FROM messages
87
116
  WHERE topic = :topic
88
- AND timestamp > :last_read_time
117
+ AND timestamp > :last_read_time
89
118
  ) a
90
119
  WHERE topic = :topic
91
120
  AND timestamp = a.min_time
@@ -108,9 +137,27 @@ AND timestamp = a.min_time
108
137
  message = ""
109
138
 
110
139
  websocket.send(json.dumps({"message": message}))
140
+ elif msg["action"] == "shutdown":
141
+ # Run this from a separate thread to prevent deadlock
142
+ global dsmq_server
143
+ print("Shutting down the dsmq server.")
144
+
145
+ def shutdown_gracefully(server_to_shutdown):
146
+ server_to_shutdown.shutdown()
147
+ time.sleep(_pause)
148
+
149
+ filenames = os.listdir()
150
+ for filename in filenames:
151
+ if filename[: len(_db_name)] == _db_name:
152
+ try:
153
+ os.remove(filename)
154
+ except FileNotFoundError:
155
+ pass
156
+
157
+ Thread(target=shutdown_gracefully, args=(dsmq_server,)).start()
158
+ sqlite_conn.close()
111
159
  else:
112
- print("Action must either be 'put' or 'get'")
113
-
160
+ print("Action must either be 'put', 'get', or 'shudown'")
114
161
 
115
162
  # Periodically clean out messages from the queue that are
116
163
  # past their sell buy date.
@@ -121,7 +168,7 @@ AND timestamp = a.min_time
121
168
  DELETE FROM messages
122
169
  WHERE timestamp < :time_threshold
123
170
  """,
124
- {"time_threshold": time_of_last_purge}
171
+ {"time_threshold": time_of_last_purge},
125
172
  )
126
173
  sqlite_conn.commit()
127
174
  time_of_last_purge = time.time()
dsmq/tests/__init__.py ADDED
File without changes
@@ -0,0 +1,238 @@
1
+ import multiprocessing as mp
2
+ import time
3
+ from dsmq.server import serve
4
+ from dsmq.client import connect
5
+
6
+ # spawn is the default method on macOS
7
+ # mp.set_start_method('spawn')
8
+
9
+ host = "127.0.0.1"
10
+ port = 30303
11
+
12
+ _short_pause = 0.001
13
+ _pause = 0.01
14
+ _long_pause = 0.1
15
+ _very_long_pause = 0.1
16
+
17
+
18
+ def test_client_server():
19
+ p_server = mp.Process(target=serve, args=(host, port))
20
+ p_server.start()
21
+ mq = connect(host, port)
22
+ read_completes = False
23
+ write_completes = False
24
+
25
+ n_iter = 11
26
+ for i in range(n_iter):
27
+ mq.put("test", f"msg_{i}")
28
+ write_completes = True
29
+
30
+ for i in range(n_iter):
31
+ msg = mq.get("test")
32
+ read_completes = True
33
+
34
+ assert msg
35
+ assert write_completes
36
+ assert read_completes
37
+
38
+ mq.shutdown_server()
39
+ mq.close()
40
+
41
+ # It takes a sec to shut down the server
42
+ time.sleep(_long_pause)
43
+ assert not p_server.is_alive()
44
+
45
+
46
+ def test_write_one_read_one():
47
+ p_server = mp.Process(target=serve, args=(host, port))
48
+ p_server.start()
49
+ write_client = connect(host, port)
50
+ read_client = connect(host, port)
51
+
52
+ write_client.put("test", "test_msg")
53
+
54
+ # It takes a moment for the write to complete
55
+ time.sleep(_pause)
56
+ msg = read_client.get("test")
57
+
58
+ assert msg == "test_msg"
59
+
60
+ write_client.shutdown_server()
61
+ write_client.close()
62
+ read_client.close()
63
+
64
+
65
+ def test_get_wait():
66
+ p_server = mp.Process(target=serve, args=(host, port))
67
+ p_server.start()
68
+ write_client = connect(host, port)
69
+ read_client = connect(host, port)
70
+
71
+ write_client.put("test", "test_msg")
72
+
73
+ msg = read_client.get_wait("test")
74
+
75
+ assert msg == "test_msg"
76
+
77
+ write_client.shutdown_server()
78
+ write_client.close()
79
+ read_client.close()
80
+
81
+
82
+ def test_multitopics():
83
+ p_server = mp.Process(target=serve, args=(host, port))
84
+ p_server.start()
85
+ write_client = connect(host, port)
86
+ read_client = connect(host, port)
87
+
88
+ write_client.put("test_A", "test_msg_A")
89
+ write_client.put("test_B", "test_msg_B")
90
+
91
+ msg_A = read_client.get_wait("test_A")
92
+ msg_B = read_client.get_wait("test_B")
93
+
94
+ assert msg_A == "test_msg_A"
95
+ assert msg_B == "test_msg_B"
96
+
97
+ write_client.shutdown_server()
98
+ write_client.close()
99
+ read_client.close()
100
+
101
+
102
+ def test_client_history_cutoff():
103
+ p_server = mp.Process(target=serve, args=(host, port))
104
+ p_server.start()
105
+ write_client = connect(host, port)
106
+ write_client.put("test", "test_msg")
107
+ time.sleep(_pause)
108
+
109
+ read_client = connect(host, port)
110
+ msg = read_client.get("test")
111
+
112
+ assert msg == ""
113
+
114
+ write_client.shutdown_server()
115
+ write_client.close()
116
+ read_client.close()
117
+
118
+
119
+ def test_two_write_clients():
120
+ p_server = mp.Process(target=serve, args=(host, port))
121
+ p_server.start()
122
+ write_client_A = connect(host, port)
123
+ write_client_B = connect(host, port)
124
+ read_client = connect(host, port)
125
+
126
+ write_client_A.put("test", "test_msg_A")
127
+ # Wait briefly, to ensure the order of writes
128
+ time.sleep(_pause)
129
+ write_client_B.put("test", "test_msg_B")
130
+ msg_A = read_client.get_wait("test")
131
+ msg_B = read_client.get_wait("test")
132
+
133
+ assert msg_A == "test_msg_A"
134
+ assert msg_B == "test_msg_B"
135
+
136
+ write_client_A.shutdown_server()
137
+ write_client_A.close()
138
+ write_client_B.close()
139
+ read_client.close()
140
+
141
+
142
+ def test_two_read_clients():
143
+ p_server = mp.Process(target=serve, args=(host, port))
144
+ p_server.start()
145
+ write_client = connect(host, port)
146
+ read_client_A = connect(host, port)
147
+ read_client_B = connect(host, port)
148
+
149
+ write_client.put("test", "test_msg")
150
+ msg_A = read_client_A.get_wait("test")
151
+ msg_B = read_client_B.get_wait("test")
152
+
153
+ assert msg_A == "test_msg"
154
+ assert msg_B == "test_msg"
155
+
156
+ write_client.shutdown_server()
157
+ write_client.close()
158
+ read_client_A.close()
159
+ read_client_B.close()
160
+
161
+
162
+ def speed_write(stop_flag):
163
+ fast_write_client = connect(host, port)
164
+ while True:
165
+ fast_write_client.put("speed_test", "speed")
166
+ if stop_flag.is_set():
167
+ break
168
+ fast_write_client.close()
169
+
170
+
171
+ def test_speed_writing():
172
+ p_server = mp.Process(target=serve, args=(host, port))
173
+ p_server.start()
174
+ write_client = connect(host, port)
175
+ read_client = connect(host, port)
176
+
177
+ stop_flag = mp.Event()
178
+ p_speed_write = mp.Process(target=speed_write, args=(stop_flag,))
179
+ p_speed_write.start()
180
+ time.sleep(_pause)
181
+
182
+ # time_a = time.time()
183
+ write_client.put("test", "test_msg")
184
+ # time_b = time.time()
185
+ msg = read_client.get_wait("test")
186
+ # time_c = time.time()
187
+
188
+ # write_time = int((time_b - time_a) * 1e6)
189
+ # read_time = int((time_c - time_b) * 1e6)
190
+ # print(f"write time: {write_time} us, read time: {read_time} us")
191
+
192
+ assert msg == "test_msg"
193
+
194
+ stop_flag.set()
195
+ p_speed_write.join()
196
+ read_client.close()
197
+ write_client.shutdown_server()
198
+ write_client.close()
199
+
200
+
201
+ def speed_read(stop_flag):
202
+ fast_read_client = connect(host, port)
203
+ while True:
204
+ fast_read_client.get("speed_test")
205
+ if stop_flag.is_set():
206
+ break
207
+ fast_read_client.close()
208
+
209
+
210
+ def test_speed_reading():
211
+ p_server = mp.Process(target=serve, args=(host, port))
212
+ p_server.start()
213
+ write_client = connect(host, port)
214
+ read_client = connect(host, port)
215
+
216
+ stop_flag = mp.Event()
217
+ p_speed_read = mp.Process(target=speed_read, args=(stop_flag,))
218
+ p_speed_read.start()
219
+
220
+ # time_a = time.time()
221
+ write_client.put("test", "test_msg")
222
+ # time_b = time.time()
223
+ msg = read_client.get_wait("test")
224
+ # time_c = time.time()
225
+
226
+ # write_time = int((time_b - time_a) * 1e6)
227
+ # read_time = int((time_c - time_b) * 1e6)
228
+ # print(f"write time: {write_time} us, read time: {read_time} us")
229
+
230
+ assert msg == "test_msg"
231
+
232
+ stop_flag.set()
233
+ p_speed_read.join()
234
+ p_speed_read.kill()
235
+ read_client.close()
236
+ write_client.shutdown_server()
237
+ # time.sleep(_pause)
238
+ write_client.close()
@@ -1,9 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dsmq
3
- Version: 1.0.0
3
+ Version: 1.1.0
4
4
  Summary: A dead simple message queue
5
5
  License-File: LICENSE
6
6
  Requires-Python: >=3.10
7
+ Requires-Dist: pytest>=8.3.4
7
8
  Requires-Dist: websockets>=14.1
8
9
  Description-Content-Type: text/markdown
9
10
 
@@ -86,7 +87,7 @@ p_mq.close()
86
87
  1. In the second, run `src/dsmq/example_put_client.py`.
87
88
  1. In the third, run `src/dsmq/example_get_client.py`.
88
89
 
89
- Alternatively, if you're on Linux just run `src/dsmq/demo_linux.py`.
90
+ Alternatively, you can run them all at once with `src/dsmq/demo.py`.
90
91
 
91
92
  ## How it works
92
93
 
@@ -150,3 +151,30 @@ connected to the server.
150
151
  - returns str, the content of the message. If there was no eligble message
151
152
  in the topic, or the topic doesn't yet exist,
152
153
  returns `""`.
154
+
155
+ ### `get_wait(topic)`
156
+
157
+ A variant of `get()` that retries a few times until it gets
158
+ a non-empty message. Adjust internal values `_n_tries` and `_initial_retry`
159
+ to change how persistent it will be.
160
+
161
+ - `topic` (str)
162
+ - returns str, the content of the message. If there was no eligble message
163
+ in the topic after the allotted number of tries,
164
+ or the topic doesn't yet exist,
165
+ returns `""`.
166
+
167
+ ### `shutdown_server()`
168
+
169
+ Gracefully shut down the server, through the client connection.
170
+
171
+ ### `close()`
172
+
173
+ Gracefully shut down the client connection.
174
+
175
+ # Testing
176
+
177
+ Run all the tests in `src/dsmq/tests/` with pytest, for example
178
+ ```
179
+ uv run pytest
180
+ ```
@@ -0,0 +1,12 @@
1
+ dsmq/__init__.py,sha256=YCgbnQAk8YbtHRyMcU0v2O7RdRhPhlT-vS_q40a7Q6g,50
2
+ dsmq/client.py,sha256=2i3BhpQM71j-uZlqiJvzQgywi1y07K1VgQe8i2Koi0g,2070
3
+ dsmq/demo.py,sha256=K53cC5kN7K4kNJlPq7c5OTIMHRCKTo9hYX2aIos57rU,542
4
+ dsmq/example_get_client.py,sha256=PvAsDGEAH1kVBifLVg2rx8ZxnAZmvzVCvZq13VgpLds,301
5
+ dsmq/example_put_client.py,sha256=QxDc3i7KAjjhpwxRRpI0Ke5KTNSPuBf9kkcGyTvUEaw,353
6
+ dsmq/server.py,sha256=iiaa7IoxZy1143vwTO7eMD-kzUKTTVff37E3KPpJ4cU,6010
7
+ dsmq/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ dsmq/tests/integration_test.py,sha256=TJyx_wsFWeSvYuMF7vCNSGcWstTSLZbuAeKrxrQcj98,5930
9
+ dsmq-1.1.0.dist-info/METADATA,sha256=S72f4YPsNO07F8W4cnrhGGEW-4Eoao9BBh2VP28uziM,4730
10
+ dsmq-1.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
11
+ dsmq-1.1.0.dist-info/licenses/LICENSE,sha256=3Yu1mAp5VsKmnDtzkiOY7BdmrLeNwwZ3t6iWaLnlL0Y,1071
12
+ dsmq-1.1.0.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- dsmq/__init__.py,sha256=YCgbnQAk8YbtHRyMcU0v2O7RdRhPhlT-vS_q40a7Q6g,50
2
- dsmq/client.py,sha256=sgkz7iabc8hirq0HRfhqlKCPwHVYgvg2C01aoRbvTSo,768
3
- dsmq/demo_linux.py,sha256=O5B2DH9pAvCCaYSRE5DV1o2fSx_bXU9hcxTis0ier5I,648
4
- dsmq/example_get_client.py,sha256=PvAsDGEAH1kVBifLVg2rx8ZxnAZmvzVCvZq13VgpLds,301
5
- dsmq/example_put_client.py,sha256=QxDc3i7KAjjhpwxRRpI0Ke5KTNSPuBf9kkcGyTvUEaw,353
6
- dsmq/server.py,sha256=fFM8soN4y8CULX5yk3Temir-pl2K1hp38MOEwqzPN5U,4343
7
- dsmq-1.0.0.dist-info/METADATA,sha256=TaoyVGkRJasSK-D6UQ9vNTeY07gOH9At7j1rZgiFxp0,4069
8
- dsmq-1.0.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
- dsmq-1.0.0.dist-info/licenses/LICENSE,sha256=3Yu1mAp5VsKmnDtzkiOY7BdmrLeNwwZ3t6iWaLnlL0Y,1071
10
- dsmq-1.0.0.dist-info/RECORD,,
File without changes