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

Add worker_class argument to LocalCluster #2625

Merged
merged 4 commits into from
Apr 20, 2019
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
12 changes: 8 additions & 4 deletions distributed/deploy/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class LocalCluster(Cluster):
Protocol to use like ``tcp://``, ``tls://``, ``inproc://``
This defaults to sensible choice given other keyword arguments like
``processes`` and ``security``
worker_class: Worker
Worker class used to instantiate workers from.

Examples
--------
Expand Down Expand Up @@ -115,6 +117,7 @@ def __init__(
security=None,
protocol=None,
blocked_handlers=None,
worker_class=None,
**worker_kwargs
):
if start is not None:
Expand Down Expand Up @@ -203,6 +206,10 @@ def __init__(
if security:
self.worker_kwargs["security"] = security

if not worker_class:
worker_class = Worker if not processes else Nanny
self.worker_class = worker_class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.worker_class = worker_class
if not worker_class:
if processes:
worker_class = Nanny
else:
worker_class = Worker
self.worker_class = worker_class


self.start(ip=ip, n_workers=n_workers)

clusters_to_close.add(self)
Expand Down Expand Up @@ -279,12 +286,9 @@ def _start_worker(self, death_timeout=60, **kwargs):
return

if self.processes:
W = Nanny
kwargs["quiet"] = True
else:
W = Worker

w = yield W(
w = yield self.worker_class(
self.scheduler.address,
loop=self.loop,
death_timeout=death_timeout,
Expand Down
30 changes: 30 additions & 0 deletions distributed/deploy/tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,5 +742,35 @@ def test_protocol_ip(loop):
assert cluster.scheduler.address.startswith("tcp://127.0.0.2")


class MyWorker(Worker):
pass


def test_worker_class_worker(loop):
with LocalCluster(
n_workers=2,
loop=loop,
worker_class=MyWorker,
processes=False,
scheduler_port=0,
dashboard_address=None,
) as cluster:
assert all(isinstance(w, MyWorker) for w in cluster.workers)


def test_worker_class_nanny(loop):
class MyNanny(Nanny):
pass

with LocalCluster(
n_workers=2,
loop=loop,
worker_class=MyNanny,
scheduler_port=0,
dashboard_address=None,
) as cluster:
assert all(isinstance(w, MyNanny) for w in cluster.workers)


if sys.version_info >= (3, 5):
from distributed.deploy.tests.py3_test_deploy import * # noqa F401