f#-async

Error F# - c# async calls : converting Threading.Tasks.Task<MyType> to Async<'a>

这一生的挚爱 提交于 2020-02-24 05:06:09
问题 When I try to call an async method that is in C# library from my F# code. I get the following compilation error. This expression was expected to have type Async<'a> but here has type Threading.Thread.Tasks.Task SendMessageAsync is in C# library and returns Threading.Thread.Tasks.Task<MyType> let sendEmailAsync message = async { let! response = client.SendMessageAsync(message) return response } 回答1: For converting between Task<'T> and Async<'T> there is a built-in Async.AwaitTask function. To

F# How Async<'T> cancellation works?

我怕爱的太早我们不能终老 提交于 2019-12-01 03:16:09
I was pretty comfortable with how async cancellations where done in C# with the TPL, but I am a little bit confused in F#. Apparently by calling Async.CancelDefaultToken() is enough to cancel outgoing Async<'T> operations. But they are not cancelled as I expected, they just... vanishes... I cannot detect properly the cancellation and tear down the stack properly. For example, I have this code that depends on a C# library that uses TPL: type WebSocketListener with member x.AsyncAcceptWebSocket = async { let! client = Async.AwaitTask <| x.AcceptWebSocketAsync Async.DefaultCancellationToken if

F# How Async<'T> cancellation works?

夙愿已清 提交于 2019-11-30 23:29:56
问题 I was pretty comfortable with how async cancellations where done in C# with the TPL, but I am a little bit confused in F#. Apparently by calling Async.CancelDefaultToken() is enough to cancel outgoing Async<'T> operations. But they are not cancelled as I expected, they just... vanishes... I cannot detect properly the cancellation and tear down the stack properly. For example, I have this code that depends on a C# library that uses TPL: type WebSocketListener with member x.AsyncAcceptWebSocket

Async.Await not catching Task exception

半世苍凉 提交于 2019-11-30 13:07:23
I have a Task that does not return anything. You can't do an Async.AwaitTask on such a Task, so you need to do an Async.AwaitIAsyncTask instead. Unfortunately this seems to just swallow any exceptions that the underlying Task throws out: - TaskFactory().StartNew(Action(fun _ -> failwith "oops")) |> Async.AwaitIAsyncResult |> Async.Ignore |> Async.RunSynchronously // val it : unit = () On the other hand, AwaitTask correctly cascades the exception: - TaskFactory().StartNew(fun _ -> failwith "oops" 5) |> Async.AwaitTask |> Async.Ignore |> Async.RunSynchronously // POP! What's the best way of

Async.Await not catching Task exception

痴心易碎 提交于 2019-11-29 19:05:13
问题 I have a Task that does not return anything. You can't do an Async.AwaitTask on such a Task, so you need to do an Async.AwaitIAsyncTask instead. Unfortunately this seems to just swallow any exceptions that the underlying Task throws out: - TaskFactory().StartNew(Action(fun _ -> failwith "oops")) |> Async.AwaitIAsyncResult |> Async.Ignore |> Async.RunSynchronously // val it : unit = () On the other hand, AwaitTask correctly cascades the exception: - TaskFactory().StartNew(fun _ -> failwith

Scheduling with Async.Parallel

。_饼干妹妹 提交于 2019-11-28 22:09:59
Is there any way that Async.Parallel can be limited/ throttled by introducing a scheduler? I'm looking to execute a Seq of Async<'a> in parallel but don't want to exceed a certain hourly-limit. I could use a shared mutable variable that each Async<'a> examines but I'd like to avoid this if possible. Under the cover, the Async.Parallel operation uses the standard .NET thread pool. So, you could configure the thread pool, but that's probably not a good idea (you should not be blocking threads in a thread pool). If I wanted to implement some throttling, I would probably create an F# agent for

Scheduling with Async.Parallel

妖精的绣舞 提交于 2019-11-27 14:14:36
问题 Is there any way that Async.Parallel can be limited/ throttled by introducing a scheduler? I'm looking to execute a Seq of Async<'a> in parallel but don't want to exceed a certain hourly-limit. I could use a shared mutable variable that each Async<'a> examines but I'd like to avoid this if possible. 回答1: Under the cover, the Async.Parallel operation uses the standard .NET thread pool. So, you could configure the thread pool, but that's probably not a good idea (you should not be blocking