Kotlin-带循环的协程 [英] Kotlin - Coroutines with loops

查看:1368
本文介绍了Kotlin-带循环的协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个简单的算法,该算法以这种方式遵循树形结构:

So, I have a simple algorithm that follows a tree structure in this manner:

每次它从一个节点移动到下一个节点时,它都会将前一个节点的属性传播到下一个节点,依此类推,以模拟节点之间的相互影响.

Each time it moves from one node to the next, it propagates attributes of the previous node to the next node and so on, to simulate the effects that the nodes have on each other.

有时一个节点可能连接到多个节点.

Sometimes a node may be connected to more than one node.

在我当前的实现中,算法会在完成树的其余部分之前遵循每个拆分路径的末尾:

With my current implementation the algorithm follows each split path to the end before completing the rest of the tree:

这是次优的,因为所有其他分支都必须等待算法完成,这会浪费很多时间,尤其是在树很大的情况下.

This is sub-optimal, as all the other branches have to wait for the algorithm to finish, which is a lot of wasted time, especially if the tree is very large.

理想情况下,我希望每个拆分都产生一个新线程,以便并行浏览所有路由.

Ideally I would want each split to spawn a new thread, so that all the routes will be explored in parallel.

我目前不是Kotlin协程的新手,所以如果这看起来很愚蠢,请多包涵.

I am currently new to Kotlin's coroutines, so please bear with me if this seems stupid.

目前,我正在考虑使用Kotlin的协程以以下方式实现此目的(注意:这是近似代码):

Currently, I am thinking of implementing this in the following way using Kotlin's coroutines (Note: This is approximate code):

suspend fun propagate(startFromNode: Node) {
   coroutineScope {
      while (true) {
        //Do propagation
        if (split) {
           launch {
              propagate(splitNode)
           }
        }
        if (atEndOfPath) {
           break
        }
      }
   }
}

我不确定Kotlin如何处理协程还可以产生新协程的情况.

I am unsure how Kotlin handles a situation where coroutines can also spawn new coroutines.

如果一个协程因某种原因引发异常,是否会取消所有源自此主要协程范围的其他协程,包括已由其他协程启动的协程?

If one coroutine throws an exception for some reason, will all the other coroutines that originate from this main coroutine scope be canceled, including the coroutines that have been started by other coroutines?

此外,如果可能的话,我想使用递归函数来实现这一点,但是似乎没有一种简单的方法可以对协程进行操作.

Also, I would like to achieve this using a recursive function if possible, but it doesn't seem like there is an easy way to do that with coroutines.

谢谢.

推荐答案

如果阅读有关结构化并发的内容,则可以找到有关此内容的更多详细信息. 但是要回答您的直接问题.

You can find out more details about this if read about structured concurrency. But to answer your immediate questions.

您的实现看起来就像我自己写的(可能是大多数人).递归似乎是解决问题的方法,并且在您完成之后也是可能的.

Your implementation looks like what I would've written myself (and probably most people). Recursion seems to be the way to go here and is possible as you've done it.

是的!每次对propagate的调用都将等待子协程完成后再返回,因此,当其中一个孩子抛出异常时,父级和同级对象将被取消(例外). coroutineScope然后将引发异常,该异常通常会取消整个协程堆栈.

Yes! Every call to propagate will wait for it's children coroutines to finish before returning, so when one of the children throws an exception the parent and siblings are cancelled (exceptionally). coroutineScope would then throw the exception, which mostly cancels the entire coroutine stack.

这篇关于Kotlin-带循环的协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆