Skip to content

Commit

Permalink
Get rid of GlobalScope
Browse files Browse the repository at this point in the history
  • Loading branch information
ileasile committed Sep 6, 2021
1 parent 42dce7a commit 948e92e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/main/kotlin/org/jetbrains/kotlinx/jupyter/connection.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.jetbrains.kotlinx.jupyter

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -147,6 +150,7 @@ class JupyterConnection(val config: KernelConfig) : Closeable {
var contextMessage: Message? = null

private val currentExecutions = HashSet<Thread>()
private val coroutineScope = CoroutineScope(Dispatchers.Default)

data class ConnectionExecutionResult<T>(
val result: T?,
Expand Down Expand Up @@ -189,6 +193,10 @@ class JupyterConnection(val config: KernelConfig) : Closeable {
}
}

fun launchJob(runnable: suspend CoroutineScope.() -> Unit) {
coroutineScope.launch(block = runnable)
}

override fun close() {
heartbeat.close()
shell.close()
Expand Down
7 changes: 2 additions & 5 deletions src/main/kotlin/org/jetbrains/kotlinx/jupyter/protocol.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.jetbrains.kotlinx.jupyter

import ch.qos.logback.classic.Level
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.encodeToJsonElement
Expand Down Expand Up @@ -300,14 +297,14 @@ fun JupyterConnection.Socket.shellMessagesHandler(msg: Message, repl: ReplForJup
sendWrapped(msg, makeReplyMessage(msg, MessageType.COMM_INFO_REPLY, content = CommInfoReply(mapOf())))
}
is CompleteRequest -> {
GlobalScope.launch(Dispatchers.Default) {
connection.launchJob {
repl.complete(content.code, content.cursorPos) { result ->
sendWrapped(msg, makeReplyMessage(msg, MessageType.COMPLETE_REPLY, content = result.message))
}
}
}
is ListErrorsRequest -> {
GlobalScope.launch(Dispatchers.Default) {
connection.launchJob {
repl.listErrors(content.code) { result ->
sendWrapped(msg, makeReplyMessage(msg, MessageType.LIST_ERRORS_REPLY, content = result.message))
}
Expand Down

0 comments on commit 948e92e

Please sign in to comment.