Async CTP and timeouts
Asked Answered
B

1

7

I started watching Jon Skeet's presentation on C# Async CTP. He stuttered when it came to specifying timeouts.

Coming from fairly limited exposure to F#, there is an intuitive, centralized, and simple way to specify timeouts. So, I am wondering what is the current state of affairs: can C# Async CTP do all the things that F# async block runner does? Is there a document that outlines differences and limitations?


Additional details: In F#, the async block runner provides a way to specify the following:

  1. Exception flow
  2. Timeout flow
  3. Cancellation flow
  4. Extensibility to the above three features

Here's a way to do these things in F#: Order of arguments and pipe-right operator

Beret answered 5/5, 2011 at 5:22 Comment(4)
Note that async workflows in F# also expose tail calls (return! to another workflow) which is of critical importance. Does C# have this?Landtag
@Jon Harrop: i only know that C# does not have tail-call optimization. I don't believe it's about to change for 5.0.Beret
@Jon Harrop: after watching the following discussion (channel9.msdn.com/Shows/Going+Deep/…), I walked away with an impression that you can compose async calls in chains. But after talking to Jon here, I am not so sure. Probably worth a separate Q.Beret
They do actually have a full implementation of trampolines in the C# CTP and that can be used to emulate tail calls so it is possible that they will or do use it to implement tail call elimination when invoking one async from another. However, I'm not sure they do and, even if they do, trampolines are notoriously slow.Landtag
L
3

I don't even remember mentioning timeouts - but I'll take your word for it :)

It's fairly easy to compose tasks to achieve a timeout: create a second task which is a "delay", and then wait for either that or the original task to complete. Whichever one gets there first, cancel the other if feasible (with a cancellation token). The newly created task will complete with either the result of the main operation (if that succeeded) or an exception if the "delay" finished first.

I don't see anything like that directly supported in AsyncCtpLibrary.dll, but you can build it reasonably easily from the tools which are provided. You may want to look in the "Task-Based Asynchronous Pattern Overview" and "TPL Dataflow" documents to see if they cover it, too.

Landscape answered 5/5, 2011 at 5:36 Comment(9)
Thanks for a quick reply. So, you have a recipe that "calls" a dozen async methods in a chain. This would mean that I would have to do what, exactly?Beret
@GregC: Are the twelve in parallel, or in series? If they're in parallel, you can use WhenAll to convert those 12 tasks into 1 effectively, and then use the suggestion in the answer. If they're in series, the async method will return a task representing the whole operation - which you can then apply the same suggestion to.Landscape
@Jon Skeet: in the F# example, you can control whether the operations listed in the AsyncBuilder monadic block are running in parallel or in series, externally. Food for thought.Beret
@GregC: In C# async that would usually be clear from the simple flow - if you create two tasks and then await them both, it'll be in parallel. If you create one and await it, then create another then await it, they'll be in series. I suspect the crucial difference here is that tasks in the Task-based async pattern are hot, whereas IIRC in F# async they're cold. This definitely changes how things are expressed in a way which could be confusing if you're very comfortable with one way and then see the other :)Landscape
@Jon Skeet: that's the key difference. Thank you for helping me gain better understanding of the subject.Beret
@Jon Skeet: "...then wait for either that or the original task to complete". How?Landtag
@Jon: Using the WhenAny method in the CTP, which returns a task which completes when any of the given tasks finish.Landscape
@Jon Skeet: I see, thanks. Is there documentation for those functions anywhere on the web? I Googled for System.Threading.Tasks.TaskEx but couldn't find anything...Landtag
@Jon: I think you'd find anything available on msdn.microsoft.com/en-us/vstudio/gg316360 - you might want to check out the samples which get installed with the CTP.Landscape

© 2022 - 2024 — McMap. All rights reserved.