Skip to content

Commit 3e5b506

Browse files
authored
Support async preload click commands (#4170)
1 parent a0b2a60 commit 3e5b506

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

distributed/preloading.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ async def start(self):
172172
context = dask_setup.make_context(
173173
"dask_setup", list(self.argv), allow_extra_args=False
174174
)
175-
dask_setup.callback(self.dask_server, *context.args, **context.params)
175+
result = dask_setup.callback(
176+
self.dask_server, *context.args, **context.params
177+
)
178+
if inspect.isawaitable(result):
179+
await result
180+
logger.info("Run preload setup click command: %s", self.name)
176181
else:
177182
future = dask_setup(self.dask_server)
178183
if inspect.isawaitable(future):

distributed/tests/test_preload.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ def check_worker():
106106
shutil.rmtree(tmpdir)
107107

108108

109+
@pytest.mark.asyncio
110+
async def test_worker_preload_click(cleanup, tmpdir):
111+
CLICK_PRELOAD_TEXT = """
112+
import click
113+
114+
@click.command()
115+
def dask_setup(worker):
116+
worker.foo = 'setup'
117+
"""
118+
async with Scheduler(port=0) as s:
119+
async with Worker(s.address, preload=CLICK_PRELOAD_TEXT) as w:
120+
assert w.foo == "setup"
121+
122+
123+
@pytest.mark.asyncio
124+
async def test_worker_preload_click_async(cleanup, tmpdir):
125+
# Ensure we allow for click commands wrapping coroutines
126+
# https://github.com/dask/distributed/issues/4169
127+
CLICK_PRELOAD_TEXT = """
128+
import click
129+
130+
@click.command()
131+
async def dask_setup(worker):
132+
worker.foo = 'setup'
133+
"""
134+
async with Scheduler(port=0) as s:
135+
async with Worker(s.address, preload=CLICK_PRELOAD_TEXT) as w:
136+
assert w.foo == "setup"
137+
138+
109139
@pytest.mark.asyncio
110140
async def test_preload_import_time(cleanup):
111141
text = """

0 commit comments

Comments
 (0)