Skip to content

Allow nested worker_client calls #9038

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

Merged
merged 2 commits into from
Apr 8, 2025
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
21 changes: 21 additions & 0 deletions distributed/tests/test_worker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,27 @@ def mysum():
assert time() < start + 3


@gen_cluster(client=True)
async def test_nested_worker_clients(c, s, a, b):
def mysum():
result = 0
with worker_client() as lc:
futures = lc.map(double, range(100))
for f in as_completed(futures):
result += f.result()
return result

def inc_mysum():
with worker_client() as lc:
future = lc.submit(inc, mysum())
result = future.result()
return result

future = c.submit(inc_mysum)
result = await future
assert result == 9901


@gen_cluster(client=True, nthreads=[("127.0.0.1", 3)])
async def test_separate_thread_false(c, s, a):
a.count = 0
Expand Down
24 changes: 14 additions & 10 deletions distributed/worker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,20 @@ def worker_client(timeout=None, separate_thread=True):
pass
else:
duration = time() - thread_state.start_time
secede() # have this thread secede from the thread pool
stack.callback(rejoin)
worker.loop.add_callback(
worker.handle_stimulus,
SecedeEvent(
key=thread_state.key,
compute_duration=duration,
stimulus_id=f"worker-client-secede-{time()}",
),
)
try:
secede() # have this thread secede from the thread pool
except KeyError: # already seceded
pass
else:
stack.callback(rejoin)
worker.loop.add_callback(
worker.handle_stimulus,
SecedeEvent(
key=thread_state.key,
compute_duration=duration,
stimulus_id=f"worker-client-secede-{time()}",
),
)

yield client

Expand Down
Loading