question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow adding a callback to a list of futures

See original GitHub issue

As discussed on gitter I’m looking for a way to asynchronously handle the result of a parallel calculation represented by a list of futures e.g. to log either the success or failure of each task.

The current api allows you to chain calls and will automatically pass the result of the tasks to the next function. If one of the tasks fails you get an exception in the calling process which prevents you handling the error asynchronously - you have to block in the calling process to handle the error

The concurrent.futures api has a Future.add_done_callback method which may be worthwhile implementing in dask.distributed

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:22 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
mrocklincommented, Oct 4, 2016

You need to use the underscore version, _wait, which is a tornado coroutine rather than a blocking function. This requires understanding of Tornado coroutines and asynchronous programming. Here is a good link:

http://www.tornadoweb.org/en/stable/coroutine.html

Example

In [1]: from distributed import Client

In [2]: c = Client()

In [3]: from distributed.client import _wait

In [4]: from tornado import gen

In [5]: def div(x, y):
   ...:     return x / y
   ...:

In [6]: future = c.submit(div, 1, 0)

In [7]: @gen.coroutine
   ...: def print_status(future):
   ...:     yield _wait(future)
   ...:     print(future.status)
   ...:

In [8]: c.loop.add_callback(print_status, future)
 error
0reactions
dhirschfeldcommented, Feb 23, 2018

I found a solution which works but is pretty ugly. I’m revisiting this problem again shortly so will open new issues if there are still some rough edges in the api.

My thoughts are to try and use the new async client and as such I’m interested in the discussion around dask/distributed#1663.

Read more comments on GitHub >

github_iconTop Results From Across the Web

First for-callback prints future list almost every time, second ...
It's because you have to explicitly block on the future. In your case, the main thread terminates before onComplete completion and sometimes ...
Read more >
Guava's Futures and ListenableFuture - Baeldung
This class provides various ways of interacting with ListenableFuture, among which is the support for adding success/failure callbacks and ...
Read more >
Futures — Python 3.11.1 documentation
Remove callback from the callbacks list. Returns the number of callbacks removed, which is typically 1, unless a callback was added more than...
Read more >
Futures (Guava: Google Core Libraries for Java 20.0 API)
Registers separate success and failure callbacks to be run when the Future 's computation is complete or, if the computation is already complete,...
Read more >
torch.futures — PyTorch master documentation
Multiple callbacks can be added to the same Future , and will be invoked in the same order as they were added. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found