Can not get client when not on worker thread
- Dominant language
- Python
- Stars
- 1.7k
- Forks
- 778
- Avg merge
- 2h 50m
- Merged PRs (30d)
- 3
Description
```python
from dask.distributed import Client, get_client
client = Client()
def f():
get_client()
return 1
client.submit(f).result() # works fine
client.run(f) # fails
```
```python-traceback
File ~/mambaforge/lib/python3.9/site-packages/distributed/client.py:2965, in Client.run(self, function, workers, wait, nanny, on_error, *args, **kwargs)
2882 def run(
2883 self,
2884 function,
(...)
2890 **kwargs,
2891 ):
2892 """
2893 Run a function on all workers outside of task scheduling system
2894
(...)
2963 >>> c.run(print_state, wait=False) # doctest: +SKIP
2964 """
-> 2965 return self.sync(
2966 self._run,
2967 function,
2968 *args,
2969 workers=workers,
2970 wait=wait,
2971 nanny=nanny,
2972 on_error=on_error,
2973 **kwargs,
2974 )
File ~/mambaforge/lib/python3.9/site-packages/distributed/utils.py:349, in SyncMethodMixin.sync(self, func, asynchronous, callback_timeout, *args, **kwargs)
347 return future
348 else:
--> 349 return sync(
350 self.loop, func, *args, callback_timeout=callback_timeout, **kwargs
351 )
File ~/mambaforge/lib/python3.9/site-packages/distributed/utils.py:416, in sync(loop, func, callback_timeout, *args, **kwargs)
414 if error:
415 typ, exc, tb = error
--> 416 raise exc.with_traceback(tb)
417 else:
418 return result
File ~/mambaforge/lib/python3.9/site-packages/distributed/utils.py:389, in sync..f()
387 future = wait_for(future, callback_timeout)
388 future = asyncio.ensure_future(future)
--> 389 result = yield future
390 except Exception:
391 error = sys.exc_info()
File ~/mambaforge/lib/python3.9/site-packages/tornado/gen.py:762, in Runner.run(self)
759 exc_info = None
761 try:
--> 762 value = future.result()
763 except Exception:
764 exc_info = sys.exc_info()
File ~/mambaforge/lib/python3.9/site-packages/distributed/client.py:2870, in Client._run(self, function, nanny, workers, wait, on_error, *args, **kwargs)
2867 continue
2869 if on_error == "raise":
-> 2870 raise exc
2871 elif on_error == "return":
2872 results[key] = exc
Input In [5], in f()
1 def f():
----> 2 get_client()
3 return 1
File ~/mambaforge/lib/python3.9/site-packages/distributed/worker.py:2782, in get_client()
2780 return Client(address, timeout=timeout)
2781 else:
-> 2782 raise ValueError("No global client found and no address provided")
ValueError: No global client found and no address provided
```
Contributor guide
Assessment
This issue has not been assessed yet.