7
7
from riak .riak_object import RiakObject
8
8
from riak .ts_object import TsObject
9
9
10
- import atexit
11
-
12
10
if PY2 :
13
11
from Queue import Queue , Empty
14
12
else :
@@ -89,7 +87,7 @@ def start(self):
89
87
name = "riak.client.multi-worker-{0}-{1}" .format (
90
88
self ._name , i )
91
89
worker = Thread (target = self ._worker_method , name = name )
92
- worker .daemon = True
90
+ worker .daemon = False
93
91
worker .start ()
94
92
self ._workers .append (worker )
95
93
self ._started .set ()
@@ -149,6 +147,11 @@ def _worker_method(self):
149
147
while not self ._should_quit ():
150
148
try :
151
149
task = self ._inq .get (block = True , timeout = 0.25 )
150
+ except TypeError :
151
+ if self ._should_quit ():
152
+ break
153
+ else :
154
+ raise
152
155
except Empty :
153
156
continue
154
157
@@ -179,6 +182,11 @@ def _worker_method(self):
179
182
while not self ._should_quit ():
180
183
try :
181
184
task = self ._inq .get (block = True , timeout = 0.25 )
185
+ except TypeError :
186
+ if self ._should_quit ():
187
+ break
188
+ else :
189
+ raise
182
190
except Empty :
183
191
continue
184
192
@@ -200,29 +208,16 @@ def _worker_method(self):
200
208
self ._inq .task_done ()
201
209
202
210
203
- RIAK_MULTIGET_POOL = MultiGetPool ()
204
- RIAK_MULTIPUT_POOL = MultiPutPool ()
205
-
206
-
207
- def stop_pools ():
208
- """Stop worker pools at exit."""
209
- RIAK_MULTIGET_POOL .stop ()
210
- RIAK_MULTIPUT_POOL .stop ()
211
-
212
-
213
- atexit .register (stop_pools )
214
-
215
-
216
211
def multiget (client , keys , ** options ):
217
212
"""Executes a parallel-fetch across multiple threads. Returns a list
218
213
containing :class:`~riak.riak_object.RiakObject` or
219
214
:class:`~riak.datatypes.Datatype` instances, or 4-tuples of
220
215
bucket-type, bucket, key, and the exception raised.
221
216
222
217
If a ``pool`` option is included, the request will use the given worker
223
- pool and not the default :data:`RIAK_MULTIGET_POOL `. This option will
224
- be passed by the client if the ``multiget_pool_size`` option was set on
225
- client initialization.
218
+ pool and not a transient :class:`~riak.client.multi.MultiGetPool `. This
219
+ option will be passed by the client if the ``multiget_pool_size``
220
+ option was set on client initialization.
226
221
227
222
:param client: the client to use
228
223
:type client: :class:`~riak.client.RiakClient`
@@ -234,26 +229,33 @@ def multiget(client, keys, **options):
234
229
:rtype: list
235
230
236
231
"""
232
+ transient_pool = False
237
233
outq = Queue ()
238
234
239
235
if 'pool' in options :
240
236
pool = options ['pool' ]
241
237
del options ['pool' ]
242
238
else :
243
- pool = RIAK_MULTIGET_POOL
244
-
245
- pool .start ()
246
- for bucket_type , bucket , key in keys :
247
- task = Task (client , outq , bucket_type , bucket , key , None , options )
248
- pool .enq (task )
249
-
250
- results = []
251
- for _ in range (len (keys )):
252
- if pool .stopped ():
253
- raise RuntimeError ("Multi-get operation interrupted by pool "
254
- "stopping!" )
255
- results .append (outq .get ())
256
- outq .task_done ()
239
+ pool = MultiGetPool ()
240
+ transient_pool = True
241
+
242
+ try :
243
+ pool .start ()
244
+ for bucket_type , bucket , key in keys :
245
+ task = Task (client , outq , bucket_type , bucket , key , None , options )
246
+ pool .enq (task )
247
+
248
+ results = []
249
+ for _ in range (len (keys )):
250
+ if pool .stopped ():
251
+ raise RuntimeError (
252
+ 'Multi-get operation interrupted by pool '
253
+ 'stopping!' )
254
+ results .append (outq .get ())
255
+ outq .task_done ()
256
+ finally :
257
+ if transient_pool :
258
+ pool .stop ()
257
259
258
260
return results
259
261
@@ -263,9 +265,9 @@ def multiput(client, objs, **options):
263
265
containing booleans or :class:`~riak.riak_object.RiakObject`
264
266
265
267
If a ``pool`` option is included, the request will use the given worker
266
- pool and not the default :data:`RIAK_MULTIPUT_POOL `. This option will
267
- be passed by the client if the ``multiput_pool_size`` option was set on
268
- client initialization.
268
+ pool and not a transient :class:`~riak.client.multi.MultiPutPool `. This
269
+ option will be passed by the client if the ``multiput_pool_size``
270
+ option was set on client initialization.
269
271
270
272
:param client: the client to use
271
273
:type client: :class:`RiakClient <riak.client.RiakClient>`
@@ -277,25 +279,32 @@ def multiput(client, objs, **options):
277
279
:type options: dict
278
280
:rtype: list
279
281
"""
282
+ transient_pool = False
280
283
outq = Queue ()
281
284
282
285
if 'pool' in options :
283
286
pool = options ['pool' ]
284
287
del options ['pool' ]
285
288
else :
286
- pool = RIAK_MULTIPUT_POOL
287
-
288
- pool .start ()
289
- for obj in objs :
290
- task = PutTask (client , outq , obj , options )
291
- pool .enq (task )
292
-
293
- results = []
294
- for _ in range (len (objs )):
295
- if pool .stopped ():
296
- raise RuntimeError ("Multi-put operation interrupted by pool "
297
- "stopping!" )
298
- results .append (outq .get ())
299
- outq .task_done ()
289
+ pool = MultiPutPool ()
290
+ transient_pool = True
291
+
292
+ try :
293
+ pool .start ()
294
+ for obj in objs :
295
+ task = PutTask (client , outq , obj , options )
296
+ pool .enq (task )
297
+
298
+ results = []
299
+ for _ in range (len (objs )):
300
+ if pool .stopped ():
301
+ raise RuntimeError (
302
+ 'Multi-put operation interrupted by pool '
303
+ 'stopping!' )
304
+ results .append (outq .get ())
305
+ outq .task_done ()
306
+ finally :
307
+ if transient_pool :
308
+ pool .stop ()
300
309
301
310
return results
0 commit comments