Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop all pools when shutting down. #488

Merged
merged 6 commits into from
Jul 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ Contributors
* Daniel Reverri
* [Dan Root](https://github.com/daroot)
* [David Basden](https://github.com/dbasden)
* [David Delassus](https://github.com/linkdd)
* David Koblas
* Dmitry Rozhkov
* Eric Florenzano
Expand Down
4 changes: 4 additions & 0 deletions RELNOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Riak Python Client Release Notes

## [2.5.5 Release](https://github.com/basho/riak-python-client/issues?q=milestone%3Ariak-python-client-2.5.5)

* [Stop all pools when client shuts down](https://github.com/basho/riak-python-client/pull/488)

## [2.5.4 Release](https://github.com/basho/riak-python-client/issues?q=milestone%3Ariak-python-client-2.5.4)

* [When converting `datetime` objects to send to Riak TS, `tzinfo` will be used if present](https://github.com/basho/riak-python-client/pull/486)
Expand Down
6 changes: 6 additions & 0 deletions riak/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ def __init__(self, protocol='pbc', transport_options={},
self._bucket_types = WeakValueDictionary()
self._tables = WeakValueDictionary()

def __del__(self):
if self._multiget_pool:
self._multiget_pool.stop()
if self._multiput_pool:
self._multiput_pool.stop()

def _get_protocol(self):
return self._protocol

Expand Down
34 changes: 27 additions & 7 deletions riak/client/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
from riak.riak_object import RiakObject
from riak.ts_object import TsObject

import atexit

if PY2:
from Queue import Queue
from Queue import Queue, Empty
else:
from queue import Queue
from queue import Queue, Empty

__all__ = ['multiget', 'multiput', 'MultiGetPool', 'MultiPutPool']

Expand Down Expand Up @@ -102,9 +104,10 @@ def stop(self):
"""
Signals the worker threads to exit and waits on them.
"""
self._stop.set()
for worker in self._workers:
worker.join()
if not self.stopped():
self._stop.set()
for worker in self._workers:
worker.join()

def stopped(self):
"""
Expand Down Expand Up @@ -144,7 +147,11 @@ def _worker_method(self):
output queue.
"""
while not self._should_quit():
task = self._inq.get()
try:
task = self._inq.get(block=True, timeout=0.25)
except Empty:
continue

try:
btype = task.client.bucket_type(task.bucket_type)
obj = btype.bucket(task.bucket).get(task.key, **task.options)
Expand All @@ -170,7 +177,11 @@ def _worker_method(self):
the output queue.
"""
while not self._should_quit():
task = self._inq.get()
try:
task = self._inq.get(block=True, timeout=0.25)
except Empty:
continue

try:
obj = task.object
if isinstance(obj, RiakObject):
Expand All @@ -193,6 +204,15 @@ def _worker_method(self):
RIAK_MULTIPUT_POOL = MultiPutPool()


def stop_pools():
"""Stop worker pools at exit."""
RIAK_MULTIGET_POOL.stop()
RIAK_MULTIPUT_POOL.stop()


atexit.register(stop_pools)


def multiget(client, keys, **options):
"""Executes a parallel-fetch across multiple threads. Returns a list
containing :class:`~riak.riak_object.RiakObject` or
Expand Down