otakusenpai
10/15/2018, 12:45 PMfun runBots() = runBlocking {
try {
bots.forEach {
val one = launch { it.value.setConnection(); }
one.join()
delay(3000L)
if(it.value.running) {
it.value.Connect()
}
}
} catch(e: Throwable) {
bots.forEach { if(!it.value.running) it.value.Connect() }
}
}
"bots" here is the map of strings to bots. Each bot has a Connect() function(a async function) which runs the all the bot's main loop. The problem is that the bot doesnt run, after the server class calls it's runBots function.
Initialised
Connnected: true
Defined bot
Added bot
[main] Connecting....
[main] Sending nick...
Sending nick: NICK Kraken3635
[main] Sending user...
Sending user: USER Nani 0 * :Nani
[main] Connected!
[DefaultDispatcher-worker-1] I'm now in Connect!
[DefaultDispatcher-worker-1] Entering loop...
enleur
10/15/2018, 1:11 PMotakusenpai
10/15/2018, 1:13 PMfun Connect() = GlobalScope.async { ... }
enleur
10/15/2018, 1:17 PMfun CoroutineScope.Connect() = launch { ... }
in this case your runBlocking
call does not exit until child launches complete whatever they dootakusenpai
10/15/2018, 1:18 PMenleur
10/15/2018, 1:19 PMotakusenpai
10/15/2018, 1:19 PMenleur
10/15/2018, 1:20 PMotakusenpai
10/15/2018, 1:20 PMgildor
10/15/2018, 1:34 PMjoin
result of connect, but to do this in parallel you can replace forEach with map and than call joinAll on list of jobsenleur
10/15/2018, 1:35 PMasync
with launch
thengildor
10/15/2018, 1:35 PMenleur
10/15/2018, 1:35 PMval one = launch { it.value.setConnection(); }
one.join()
what is the reason behind this?gildor
10/15/2018, 1:36 PMotakusenpai
10/15/2018, 1:38 PMgildor
10/15/2018, 1:39 PMotakusenpai
10/15/2018, 1:40 PMgildor
10/15/2018, 1:40 PMotakusenpai
10/15/2018, 1:40 PMgildor
10/15/2018, 1:42 PMotakusenpai
10/15/2018, 1:43 PMgildor
10/15/2018, 1:43 PMotakusenpai
10/15/2018, 1:46 PMgildor
10/15/2018, 1:47 PMThis programming style with async functions is provided here only for illustration, because it is a popular style in other programming languages. Using this style with Kotlin coroutines is strongly discouraged for the reasons that are explained below.
otakusenpai
10/15/2018, 1:48 PMgildor
10/15/2018, 1:48 PM