Repository: dask/distributed

Issue:
Rename local_client to worker_client?
The term "local", might be confusing.  Perhaps `worker_client` is more clear?  Or are there better names?


Discussion:
I agree `worker_client` is a bit clearer, since it is designed specifically to run in coordination with a worker and its inner structures.

Resolving patch:
diff --git a/distributed/__init__.py b/distributed/__init__.py
--- a/distributed/__init__.py
+++ b/distributed/__init__.py
@@ -10,7 +10,7 @@
 from .scheduler import Scheduler
 from .utils import sync
 from .worker import Worker
-from .worker_client import local_client
+from .worker_client import local_client, worker_client
 
 try:
     from .collections import futures_to_collection
diff --git a/distributed/worker_client.py b/distributed/worker_client.py
--- a/distributed/worker_client.py
+++ b/distributed/worker_client.py
@@ -18,7 +18,7 @@
 
 
 @contextmanager
-def local_client(timeout=3):
+def worker_client(timeout=3):
     """ Get client for this thread
 
     Note: This interface is new and experimental.  It may change without
@@ -32,7 +32,7 @@ def local_client(timeout=3):
     --------
 
     >>> def func(x):
-    ...     with local_client() as e:  # connect from worker back to scheduler
+    ...     with worker_client() as e:  # connect from worker back to scheduler
     ...         a = e.submit(inc, x)     # this task can submit more tasks
     ...         b = e.submit(dec, x)
     ...         result = e.gather([a, b])  # and gather results
@@ -53,6 +53,8 @@ def local_client(timeout=3):
         yield wc
 
 
+local_client = worker_client
+
 def get_worker():
     return thread_state.execution_state['worker']
 


Test patch:
diff --git a/distributed/tests/test_channels.py b/distributed/tests/test_channels.py
--- a/distributed/tests/test_channels.py
+++ b/distributed/tests/test_channels.py
@@ -8,7 +8,7 @@
 from tornado import gen
 
 from distributed import Client
-from distributed import local_client
+from distributed import worker_client
 from distributed.metrics import time
 from distributed.utils_test import gen_cluster, inc, loop, cluster, slowinc
 
@@ -52,9 +52,9 @@ def test_channel(c, s, a, b):
     assert '1' in repr(x)
 
 
-def test_local_client(loop):
+def test_worker_client(loop):
     def produce(n):
-        with local_client() as c:
+        with worker_client() as c:
             x = c.channel('x')
             for i in range(n):
                 future = c.submit(slowinc, i, delay=0.01, key='f-%d' % i)
@@ -63,7 +63,7 @@ def produce(n):
             x.flush()
 
     def consume():
-        with local_client() as c:
+        with worker_client() as c:
             x = c.channel('x')
             y = c.channel('y')
             last = 0
@@ -157,7 +157,7 @@ def test_multiple_maxlen(c, s, a, b):
 
 def test_stop(loop):
     def produce(n):
-        with local_client() as c:
+        with worker_client() as c:
             x = c.channel('x')
             for i in range(n):
                 future = c.submit(slowinc, i, delay=0.01, key='f-%d' % i)
diff --git a/distributed/tests/test_steal.py b/distributed/tests/test_steal.py
--- a/distributed/tests/test_steal.py
+++ b/distributed/tests/test_steal.py
@@ -13,7 +13,7 @@
 
 import dask
 from dask import delayed
-from distributed import Worker, Nanny, local_client
+from distributed import Worker, Nanny, worker_client
 from distributed.config import config
 from distributed.client import Client, _wait, wait
 from distributed.metrics import time
@@ -531,7 +531,7 @@ def test_accept_old_result_if_stolen(c, s, a, b):
 @gen_cluster(client=True, ncores=[('127.0.0.1', 1)] * 2)
 def test_dont_steal_long_running_tasks(c, s, a, b):
     def long(delay):
-        with local_client() as c:
+        with worker_client() as c:
             sleep(delay)
 
     yield c.submit(long, 0.1)._result()  # learn duration
diff --git a/distributed/tests/test_worker_client.py b/distributed/tests/test_worker_client.py
--- a/distributed/tests/test_worker_client.py
+++ b/distributed/tests/test_worker_client.py
@@ -5,7 +5,7 @@
 from dask import delayed
 from tornado import gen
 
-from distributed import local_client, Client, as_completed
+from distributed import worker_client, Client, as_completed
 from distributed.metrics import time
 from distributed.utils_test import gen_cluster, inc, double, cluster, loop
 
@@ -13,7 +13,7 @@
 @gen_cluster(client=True)
 def test_submit_from_worker(c, s, a, b):
     def func(x):
-        with local_client() as c:
+        with worker_client() as c:
             x = c.submit(inc, x)
             y = c.submit(double, x)
             result = x.result() + y.result()
@@ -32,7 +32,7 @@ def func(x):
 @gen_cluster(client=True, ncores=[('127.0.0.1', 1)] * 2)
 def test_scatter_from_worker(c, s, a, b):
     def func():
-        with local_client() as c:
+        with worker_client() as c:
             futures = c.scatter([1, 2, 3, 4, 5])
             assert isinstance(futures, (list, tuple))
             assert len(futures) == 5
@@ -49,7 +49,7 @@ def func():
     assert result == sum([1, 2, 3, 4, 5])
 
     def func():
-        with local_client() as c:
+        with worker_client() as c:
             correct = True
             for data in [[1, 2], (1, 2), {1, 2}]:
                 futures = c.scatter(data)
@@ -76,7 +76,7 @@ def test_gather_multi_machine(c, s, a, b):
     a_address = b.address
     b_address = b.address
     def func():
-        with local_client() as ee:
+        with worker_client() as ee:
             x = ee.submit(inc, 1, workers=a_address)
             y = ee.submit(inc, 2, workers=b_address)
 
@@ -92,7 +92,7 @@ def func():
 @gen_cluster(client=True)
 def test_same_loop(c, s, a, b):
     def f():
-        with local_client() as lc:
+        with worker_client() as lc:
             return lc.loop is lc.worker.loop
 
     future = c.submit(f)
@@ -105,7 +105,7 @@ def mysum():
         result = 0
         sub_tasks = [delayed(double)(i) for i in range(100)]
 
-        with local_client() as lc:
+        with worker_client() as lc:
             futures = lc.compute(sub_tasks)
             for f in as_completed(futures):
                 result += f.result()
@@ -122,7 +122,7 @@ def mysum():
         result = 0
         sub_tasks = [delayed(double)(i) for i in range(100)]
 
-        with local_client() as lc:
+        with worker_client() as lc:
             futures = lc.compute(sub_tasks)
             for f in as_completed(futures):
                 result += f.result()