-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
There is no way to cancel a function if we realize the work is no longer required #59903
Comments
Summary: Futures in Dart lack a cancellation mechanism. |
You need to manually implement a concept of cancellation since there are no link from a given Future to the logic that might fill out the value later on (e.g. if you use a If you look in the |
@julemand101 from my understanding the That's not what this issue is about, it is about canceling the work behind a future. |
@stephane-archer I also write why it is not feasible to have this handled automatically since we should expect any function in Dart to run to completion unless the program are terminated by force or exception are thrown. And, as also described, we don't know which function are going to complete a given Future. Yes, in this case, with a Future created from an And since Future objects can be created by other than async-marked functions, (e.g. Completer), it is going to be near impossible to get a full tree of code that we can then just "cancel" by just not run this code. This automatic "cancel" could also be very problematic if you need to tell some driver that you want to close a resource as part of the "cancel". The overall design of And |
Duplicate of #42855 |
@julemand101 could you clarify how I can make |
@stephane-archer import 'package:async/async.dart';
CancelableOperation<String> expensiveFunction() {
CancelableCompleter<String> completer = CancelableCompleter();
() async {
await Future<void>.delayed(Duration(minutes: 10));
if (!completer.isCanceled) {
print("hello world!");
}
}();
return completer.operation;
}
void main() async {
var future = expensiveFunction();
//Some events came
future.cancel();
} If we instead redesign this a bit so we get access to the timer, we can be a bit more elegant: CancelableOperation<String> expensiveFunction() {
Timer? timer;
CancelableCompleter<String> completer = CancelableCompleter(onCancel: () {
timer?.cancel();
});
timer = Timer(Duration(minutes: 10), () {
print("hello world!");
});
return completer.operation;
}
void main() async {
var future = expensiveFunction();
//Some events came
future.cancel();
} But this also shows that it can be hard to implement a design that supports cancel of operations since you might end up implement a lot of code to support this correctly. And if you do any async calls that does not support cancel, then you are forced to wait at some point. An alternative is to use isolate's which supports immediate killing. But that does still then come with the problem of missing cleanup and, as mention before, it can be problematic in some scenarios to just stop executing some code while it is running. |
There is no way to cancel a function if we realize the work is no longer required:
Right now from my understanding:
expensiveFunction
will compute on my CPU for 10 minutes even if I know I don't need this value anymore and I will see "hello world!" on the console no matter what I do.The text was updated successfully, but these errors were encountered: