F#:异步返回之前 SwitchToThreadPool 的用途
在 Async.SwitchToNewThread 的 MS 文档中,给出的示例之一是:
let asyncMethod f =
async {
do! Async.SwitchToNewThread()
let result = f()
do! Async.SwitchToThreadPool()
return result
}
在 return 语句之前切换到线程池的目的是什么?我理解为什么当异步块有更多工作要做时您可能希望从专用线程切换到线程池,但这里的情况并非如此。
这不是主要问题的一部分,但我也很好奇为什么 SwitchToNewThread 和 SwitchToThreadPool 返回 Async。是否存在您不想立即“执行!”的用例?这些任务?谢谢
In the MS docs for Async.SwitchToNewThread one of the examples given is:
let asyncMethod f =
async {
do! Async.SwitchToNewThread()
let result = f()
do! Async.SwitchToThreadPool()
return result
}
What is the purpose of switching to the thread pool immediately before a return statement? I understand why you might want to switch from a dedicated thread to the thread pool when the async block has more work to do but that is not the case here.
This is not part of the main question, but I'm also curious to know why SwitchToNewThread and SwitchToThreadPool return an Async. Is there ever a use case where you would not want to immediately "do!" these tasks? Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个例子可能更清楚,因为它没有演示任何真实场景。
然而,在
返回
之前切换到另一个线程是有充分理由的。原因是调用您的函数(例如asyncMethod
)的工作流程将继续在您返回之前切换到的上下文/线程中运行。例如,如果您编写:我认为示例中使用的模式不太正确 - 异步工作流应始终返回到启动它们的
SynchronizationContext
(例如,如果工作流启动于GUI 线程,它可以切换到新线程,但随后应返回到 GUI 线程)。如果我正在编写asyncMethod
函数,我会使用:回答你的第二个问题 -类型本质上只是一些获取函数(工作流程的其余部分)并可以在任何想要的地方执行它的对象,但没有其他方法可以“破坏”工作流程。
SwitchTo
操作返回Async
的原因和需要使用do!
调用的一个问题是没有办法直接切换到不同的线程。只有当您使用do!
或let!
时,您才能将工作流程的其余部分作为函数(可以在新线程上执行)获得。 >AsyncThe example could be clearer, because it doesn't demonstrate any real scenario.
However, there is a good reason for switching to another thread before
return
. The reason is that the workflow that calls your function (e.g.asyncMethod
) will continue running in the context/thread that you switch to before returning. For example, if you write:I think the pattern used in the example isn't quite right - asynchronous workflows should always return back to the
SynchronizationContext
on which they were started (e.g. if a workflow is started on GUI thread, it can switch to a new thread, but should then return back to the GUI thread). If I was writingasyncMethod
function, I'd use:To answer your second question - the reason why
SwitchTo
operations returnAsync<unit>
and need to be called usingdo!
is that there is no way to switch to a different thread directly. The only points where you get the rest of the workflow as a function (that you can execute on a new thread) is when you usedo!
orlet!
TheAsync<T>
type is essentially just some object that gets a function (the rest of the workflow) and can execute it anywhere it wants, but there is no other way to "break" the workflow.