carmichael561
carmichael561

Reputation: 73

get the name of the dask delayed object/task from within the original function?

Suppose I have some function

def f(x):
    y = ... # do something
    return y

that I then delay and compute:

result = dask.delayed(f, name='Jim')(x, dask_key_name='Jim')
result.compute()

Is it possible to get the name Jim from within f? Can I ask the worker for the key associated with the current task?

I found this old post and the following gets me something that might be useful but it isn't Jim, it's something generated by dask:

import dask.distributed
dask.distributed.worker.thread_state.key # Not "Jim"

Upvotes: 0

Views: 224

Answers (1)

mdurant
mdurant

Reputation: 28673

This is one way to do it:

def f(x):
    return dask.distributed.get_worker().get_current_task()

d = dask.delayed(f)(0, dask_key_name="Jim")
assert d.compute() == 'Jim'

Which internally uses the active_threads property to look up the task associated with the current thread.

Upvotes: 1

Related Questions