I'm looking to add a callback to a future once it is finished.
Per the documentation:
Call callback on future when callback has finished.
The callback fn should take the future as its only argument. This will be called regardless of if the future completes successfully, errs, or is cancelled.
The callback is executed in a separate thread.
This doesn't provide me with what I need because of the requirement for callback fn to take future as its only argument.
Here's a sample portion code of what I am tying to do:
def method(cu_device_id):
print("Hello world, I'm going to use GPU %i" % cu_device_id)
def callback_fn(cu_device_id)
gpu_queue.put(cu_device_id)
cu_device_id = gpu_queue.get()
future = client.submit(method, cu_device_id)
#gpu_queue.put(cu_device_id) # Does not work, clients will shortly end up piled onto the slowest GPU
result.add_done_callback(callback_fn) # Crash / no way to pass in cu_device_id
The idea here is to have a client take an available GPU from the queue, and then once it is finished using it, put it back into the queue so that another client can use it.
One way to get around this is to pass gpu_queue into client:
def method(gpu_queue):
cu_device_id = gpu_queue.get()
print("Hello world, I'm going to use GPU %i" % cu_device_id)
gpu_queue.put(cu_device_id)
future = client.submit(method, gpu_queue)
Things work as expected like this. But I prefer to be able to do this from outside What I missing or not seeing to make this work?
Thanks