- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑一组检查工作,每个检查工作都有独立的逻辑,因此它们似乎可以并发运行,例如:
type Work struct {
// ...
}
// This Check could be quite time-consuming
func (w *Work) Check() bool {
// return succeed or not
//...
}
func CheckAll(works []*Work) {
num := len(works)
results := make(chan bool, num)
for _, w := range works {
go func(w *Work) {
results <- w.Check()
}(w)
}
for i := 0; i < num; i++ {
if r := <-results; !r {
ReportFailed()
break;
}
}
}
func ReportFailed() {
// ...
}
当关注结果
时,如果逻辑是无论哪个工作失败,我们都断言所有工作完全失败, channel 中剩余的值是无用的。让剩余未完成的 goroutine 继续运行并将结果发送到 channel 是没有意义和浪费的,尤其是当 w.Check()
相当耗时时。理想效果类似于:
for _, w := range works {
if !w.Check() {
ReportFailed()
break;
}
}
这只运行必要的检查工作然后中断,但在顺序非并发场景中。
那么,是否可以取消这些未完成的goroutines,或者发送到channel?
最佳答案
您最初的问题是询问如何取消发送操作。 channel 上的发送基本上是“即时的”。如果 channel 的缓冲区已满并且没有准备好的接收器,则 channel 上的发送将阻塞。
您可以通过使用select
语句和您关闭的cancel
channel “取消”此发送,例如:
cancel := make(chan struct{})
select {
case ch <- value:
case <- cancel:
}
在另一个 goroutine 上使用 close(cancel)
关闭 cancel
channel 将使上面的选择放弃 ch
上的发送(如果它阻塞).
但如前所述,发送在“就绪” channel 上是“即时的”,发送首先评估要发送的值:
results <- w.Check()
这首先必须运行 w.Check()
,一旦完成,它的返回值将在 results
上发送。
所以你真正需要的是取消w.Check()
方法调用。为此,惯用的方法是传递一个可以取消的 context.Context
值,并且 w.Check()
本身必须监视并“服从”这个取消请求。
参见 Terminating function execution if a context is cancelled
请注意,您的函数必须明确支持这一点。函数调用或 goroutines 没有隐式终止,参见 cancel a blocking operation in Go .
所以你的 Check()
应该看起来像这样:
// This Check could be quite time-consuming
func (w *Work) Check(ctx context.Context, workDuration time.Duration) bool {
// Do your thing and monitor the context!
select {
case <-ctx.Done():
return false
case <-time.After(workDuration): // Simulate work
return true
case <-time.After(2500 * time.Millisecond): // Simulate failure after 2.5 sec
return false
}
}
CheckAll()
可能如下所示:
func CheckAll(works []*Work) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
num := len(works)
results := make(chan bool, num)
wg := &sync.WaitGroup{}
for i, w := range works {
workDuration := time.Second * time.Duration(i)
wg.Add(1)
go func(w *Work) {
defer wg.Done()
result := w.Check(ctx, workDuration)
// You may check and return if context is cancelled
// so result is surely not sent, I omitted it here.
select {
case results <- result:
case <-ctx.Done():
return
}
}(w)
}
go func() {
wg.Wait()
close(results) // This allows the for range over results to terminate
}()
for result := range results {
fmt.Println("Result:", result)
if !result {
cancel()
break
}
}
}
测试它:
CheckAll(make([]*Work, 10))
输出(在 Go Playground 上尝试):
Result: true
Result: true
Result: true
Result: false
我们打印了 3 次 true
(在 2.5 秒内完成),然后故障模拟开始,返回 false
,并终止所有其他作业。
请注意,上面示例中的 sync.WaitGroup
并不是严格需要的,因为 results
有一个能够保存所有结果的缓冲区,但总的来说它仍然是一个很好的做法(你将来应该使用更小的缓冲区吗?
参见相关:Close multiple goroutine if an error occurs in one in go
关于go - 是否可以取消未完成的 goroutines?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71855079/
下面的代码已经工作了一段时间,但突然停止返回: await availableChangedPublishEndpoint .Publish( AvailableStockCou
我正在尝试使用 NSMetaDataQuery 在 iCloud 中探索我的应用可用的文件。我能够开始查询,但它永远不会结束。这是我的代码。 func report1() { let file
这是我的 Gulp 任务: //Image Optimization gulp.task( 'imagemin', function () { return gulp.src( imageDi
此代码片段已编译: 让测试= 让 x = 1 printfn "%A"x 如果最后一行被删除,则会出现以下编译错误: 错误 FS0588:此“让”之后的 block 未完成。期待一个表情。 这条消息是
很抱歉一直询问有关 Powershell 的问题,我的 script-foo 不是它需要的。 我正在编写一个 BitsTransfer .PS1 来自动每周下载一个 ASCII 文件。 它似乎永远不会
我最近使用了相当多的 SwingWorkers,并且遇到了一些问题,因此我尝试创建一个 SCSSE,但这显然不起作用 static SwingWorker worker; public static
我有一个包含 DatePickerDialog 的 DialogFragment。每当我设置日期时,一项新 Activity 就会开始。当我按回去时,它会返回到相同的 Activity 。为什么 Ac
我实现了一个 PhoneStateListener 如何在您被调用时停止音频并在您完成后再次启动它。当我通过 finish() 退出我的应用程序时,PhoneStateListener 似乎仍然处于
这是建立在另一个问题上, Left join and only last row from right 我尝试使用相同的策略编写第二个连接。我在我的 dev macbook pro 上运行 postg
我正在使用 XMLHttpRequest 调用 PHP 文件,但现在调用未完成,我不知道为什么。 req.readyState 不是 4,我不知道为什么,因为 PHP 文件没问题,并且完全执行了预期的
我需要为一系列图像制作动画。 // Animate Circle let duration = 1.0 circleGroup.setBackgroundImageNamed("arc") circl
此方法永远不会通过 foreach 循环。无论我在它后面放什么,它都不会运行。即使 book 对象中没有联系人对象,它也不应该跳过其余代码。这被用于我正在开发的应用程序中的联系人选择器,关于这里发生的
我刚刚开始学习 python,我想看看是否可以制作 monty Hall 问题的 python 版本。当我使用 1 或 2 次迭代时,一切似乎都在小范围内工作,但超过此后,一切都不起作用了。 for
是否有任何原因导致 Activity 可能未完成(即调用其 finalize())并因此被垃圾收集,即使它的 onDestroy() 已被调用?这只是您每天的平均内存泄漏吗? 最佳答案 onDestr
我正在尝试访问 Twitter 帐户,但此代码在 accountTypeWithAccountTypeIdentifier 行停止。 我的代码看起来像这样,我运行的是 iOS 6: #import
这个问题在这里已经有了答案: An async/await example that causes a deadlock (5 个回答) 4年前关闭。 我是 C# 任务的新手,遇到了一个我不理解的问题
我正在尝试在 linux 服务器上以非 GUI 模式运行 JMX 文件。使用 SFTP 协议(protocol)尝试将文件上传到对象存储。下面是脚本和用于运行脚本的配置。 测试.jmx import
我正在尝试等待 getJson 调用并处理返回的数据。我看过很多答案,并尝试使用 $.ajax 的 async: false(不起作用,因为查询是跨域的)、回调和.done 基本 $.ajax 调用的
我有一个带有 echo 的 php 代码来检查它停止的位置(它不会崩溃,但会停止发送 echo 并且不能按预期工作) $stmt=$conexion->prepare("SELECT Email, M
我是 Scala 的新手,我想知道是否有一些方法可以创建一个虚拟的 Future 对象来模拟 isCompleted 到 false。我需要这个用于测试目的。 目前我用来制作虚拟Future[Int]
我是一名优秀的程序员,十分优秀!