async/await 取消机制

发布于 2024-10-16 07:16:02 字数 158 浏览 4 评论 0原文

我有一个问题,关于可以使用什么机制来取消正在进行的异步操作,而不是使用异步/等待上下文中的取消令牌。我确信这是一个经过充分研究的设计决策,考虑到了语言的命令性,但在实际情况下,必须将取消对象传递给所有异步方法至少有点痛苦。 c#社区还有另外的设计思路,或者提出的取消机制就可以了?我想我错过了一些东西。

I have a question about what mechanism to cancel an ongoing async operation could be used, instead a cancellation token in the async/await context. I'm sure this is a well studied design decision that takes into account the imperative nature of the language, but in real situations having to pass a cancellation object to all your async methods is, at least, a bit painful. There are another design ideas from the c# community, or the proposed cancellation mechanism is just OK? I think I'm missing something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏九 2024-10-23 07:16:02

取消令牌是最佳实践,特别是当异步过程成本高昂、没有预设结束条件或涉及外部资源时。

但是,如果您愿意,您可以简单地“放弃”。不是告诉异步线程中止处理并清理,而是“超时”;停止等待它完成,分离所有侦听器,然后继续运行。当线程最终完成时,它将检查其事件,意识到没有人在监听,然后默默地终止。好处是简单,但是在很多情况下这将是一件坏事:

  • 如果异步进程将永远继续处理,除非您告诉它停止,否则它将继续在后台运行,占用 CPU 和其他资源,直到应用程序关闭,此时线程将被杀死。
  • 如果异步线程正在执行可中止的、可逆的工作单元(例如数据库操作),那么如果用户按下“取消”,他们预计到目前为止所做的任何事情都会被回滚。如果你停止聆听并继续前进,他们会得到的是他们认为取消的内容已经执行了。
  • 在大多数情况下,异步操作涉及外部资源,这些资源需要时间来处理,并且还需要适当的清理。网络套接字必须断开,数据库连接关闭,文件解锁等。尽管放弃意味着您不必处理这些问题,而是让线程正常结束,但常见的用户体验是用户感到厌倦,点击取消然后重试该操作。这意味着您在之前的异步操作中使用的资源必须被释放才能再次使用。

The cancellation token is the best practice, especially when the asynchronous process is expensive, has no preset ending condition, or involves external resources.

However, you can, if you wish, simply "give up". Instead of telling the async thread to abort processing and clean up, just "time out"; stop waiting for it to complete, detach any listeners, and keep running. When the thread eventually does complete, it'll check its event, realize nobody's listening, and silently terminate. The upside is the simplicity, however there are many situations in which this would be a BAD thing:

  • If the async process will keep processing forever unless you tell it to stop, it will keep running in the background, tying up CPU and other resources, until the app is closed, at which time the thread will be killed.
  • If the async thread is performing an abortable, reversible unit of work such as a DB operation, if the user presses cancel they expect anything done so far to have been rolled back. If you stop listening and move on, what they'll get is that what they thought they cancelled was performed.
  • In most cases, async operations involve external resources which take time to work with, and also require proper cleanup. Network sockets must be disconnected, DB connections closed, files unlocked, etc. Although giving up means you don't have to deal with that, instead letting the thread end normally, a common user experience is for the user to get fed up, hit cancel and then retry the operation. That means the resource you had used in the previous async operation has to be released to use it again.

为了使异步操作的取消有意义,该操作必须执行谨慎的步骤,因为该操作需要检查取消令牌并停止自身继续。即,取消令牌仅仅是要实现的模式,而不是异步/等待机制。

这意味着,如果您的异步操作只是一个带有完成句柄的 I/O 调用,那么您最好放弃而不是取消,因为在该调用返回之前,该操作将没有机会检查您的令牌到那时就什么也得不到了。

因此,在考虑取消令牌时,首先考虑是否可以通过支持取消来提高此操作的效率,或者是否只是不等待它完成(即使用超时机制)更有意义。

In order for cancellation of an async operation to make sense, that operation must perform discreet steps, since it is up the operation to check the cancellation token and stop itself from continuing. I.e. the cancellation token is merely a pattern to be implemented not a mechanism of async/await.

What that means it that if your async operation is just one I/O call with a completion handle, you might as well, just give up rather than cancel, since the operation will not have the oppurtunity to check your token until that call comes back at which point there is nothing to be gained.

So when considering a cancellation token, first consider whether this operation can be made more efficient by supporting cancellation or whether just not waiting for it to complete (i.e. using a timeout mechanism instead) makes more sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文