Public » MiniRedis Read More
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

allow stopping MiniRedis via SIGTERM and keyboard
* * *

fold me

Changeset 44a5fe767209

Parent c60d1792f0e9

by Profile picture of Benjamin PollackBenjamin Pollack

Changes to one file · Browse files at 44a5fe767209 Showing diff from parent c60d1792f0e9 Diff from another changeset...

Change 1 of 6 Show Entire File miniredis.py Stacked
 
4
5
6
 
7
8
9
10
11
 
12
13
14
 
120
121
122
123
 
 
 
 
 
 
124
125
126
 
138
139
140
141
142
143
144
 
152
153
154
155
 
 
 
156
157
158
 
276
277
278
 
 
 
 
279
280
281
 
288
289
290
291
 
 
 
 
 
292
293
294
 
4
5
6
7
8
9
10
11
12
13
14
15
16
 
122
123
124
 
125
126
127
128
129
130
131
132
133
 
145
146
147
 
148
149
150
 
158
159
160
 
161
162
163
164
165
166
 
284
285
286
287
288
289
290
291
292
293
 
300
301
302
 
303
304
305
306
307
308
309
310
@@ -4,11 +4,13 @@
 from __future__ import with_statement    import datetime +import errno  import getopt  import os  import re  import pickle  import select +import signal  import socket  import sys   @@ -120,7 +122,12 @@
  server.bind((self.host, self.port))   server.listen(5)   while not self.halt: - readable, _, _ = select.select([server] + self.clients.keys(), [], [], 1.0) + try: + readable, _, _ = select.select([server] + self.clients.keys(), [], [], 1.0) + except select.error, e: + if e.args[0] == errno.EINTR: + continue + raise   for sock in readable:   if sock == server:   (client_socket, address) = server.accept() @@ -138,7 +145,6 @@
  client_socket.close()   self.clients.clear()   server.close() - server = None     def save(self):   if self.db_file: @@ -152,7 +158,9 @@
  client.table = self.tables[db]     def stop(self): - self.halt = True + if not self.halt: + self.log(None, 'STOPPING') + self.halt = True     # HANDLERS   @@ -276,6 +284,10 @@
  return self.handle_quit(client)    def main(args): + def sigterm(signum, frame): + m.stop() + signal.signal(signal.SIGTERM, sigterm) +   host, port, log_file, db_file = '127.0.0.1', 6379, None, None   opts, args = getopt.getopt(args, 'h:p:d:l:')   for o, a in opts: @@ -288,7 +300,11 @@
  elif o == '-d':   db_file = os.path.abspath(a)   m = MiniRedis(host=host, port=port, log_file=log_file, db_file=db_file) - m.run() + try: + m.run() + except KeyboardInterrupt: + m.stop() + sys.exit(0)    if __name__ == '__main__':   main(sys.argv[1:])