Skip to content

Instantly share code, notes, and snippets.

@cmelchior
Created February 18, 2026 12:47
Show Gist options
  • Select an option

  • Save cmelchior/361c59e0752c63ff37c6c22cfad9cb67 to your computer and use it in GitHub Desktop.

Select an option

Save cmelchior/361c59e0752c63ff37c6c22cfad9cb67 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "code",
"source": [
"// Without coroutine API's can still declare suspend functions (but they cannot be called)\n",
"// This works today\n",
"suspend fun main() {\n",
" println(\"Hello, world!\")\n",
"}"
],
"id": "1b714efacf22b922",
"outputs": [],
"execution_count": null
},
{
"metadata": {
"executionRelatedData": {
"compiledClasses": [
"Line_3_jupyter"
]
}
},
"cell_type": "code",
"source": [
"// Include coroutine APIs\n",
"%use coroutines"
],
"id": "ab9e008170cf47a3",
"outputs": [],
"execution_count": null
},
{
"metadata": {
"executionRelatedData": {
"compiledClasses": [
"Line_5_jupyter",
"Line_6_jupyter",
"Line_4_jupyter",
"Line_14_jupyter"
]
}
},
"cell_type": "code",
"source": [
"// Example 1: Call suspend functions\n",
"suspend fun main() {\n",
" delay(500.milliseconds)\n",
" println(\"Hello, world!\")\n",
"}\n",
"\n",
"main()"
],
"id": "17a20d47ae138999",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"// Example 2: Join multiple tasks\n",
"suspend fun performTask(name: String): String {\n",
" delay(1000L)\n",
" return \"Task $name completed\"\n",
"}\n",
"\n",
"val job1 = async { performTask(\"A\") }\n",
"val job2 = async { performTask(\"B\") }\n",
"awaitAll(job1, job2)"
],
"id": "566e203d05b2c1c2",
"outputs": [],
"execution_count": null
},
{
"metadata": {
"executionRelatedData": {
"compiledClasses": [
"Line_7_jupyter"
]
}
},
"cell_type": "code",
"source": [
"// Example 3: Cell will block until all children completes\n",
"launch {\n",
" delay(1000L)\n",
" println(\"Task A completed\")\n",
"}\n"
],
"id": "9548a52d42a8e021",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"// Example 4: Flows will block the cell if collected on the top-level\n",
"fun numbersFlow(): Flow<Int> = flow {\n",
" for (i in 1..3) {\n",
" delay(100)\n",
" println(\"Emitting $i on ${Thread.currentThread().name}\")\n",
" emit(i)\n",
" }\n",
"}.flowOn(Dispatchers.Default)\n",
"\n",
"numbersFlow().collect { value ->\n",
" println(\"Collected $value on ${Thread.currentThread().name}\")\n",
"}"
],
"id": "56f9ff19bf961c30",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"// Example 5: Exceptions should be shown as Cell output stacktraces\n",
"suspend fun performTask(name: String): String {\n",
" delay(1000L)\n",
" throw RuntimeException(\"Error performing task $name\")\n",
"}\n",
"\n",
"performTask(\"Check Error\")"
],
"id": "a3636ed41c72ab8c",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"// Example 6: runblocking should work\n",
"runBlocking {\n",
" main()\n",
"}"
],
"id": "7b1e5c36c325ae1b",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"// Example 7: Create your own scopes. Cell is blocked until it completes\n",
"coroutineScope(Dispatchers.Default) {\n",
" main()\n",
"}"
],
"id": "460ee42113cfa06d",
"outputs": [],
"execution_count": null
},
{
"metadata": {
"executionRelatedData": {
"compiledClasses": [
"Line_16_jupyter",
"Line_15_jupyter",
"Line_17_jupyter"
]
}
},
"cell_type": "code",
"source": [
"// Example 7a: There will be ways where the Cell finishes before the child scope is done\n",
"// This is similar to what can be accomplished using ThreadPools in K1 Notebooks, so probably acceptable\n",
"suspend fun work() {\n",
" delay(1000L)\n",
" println(\"Done!\")\n",
"}\n",
"suspend fun outer() = coroutineScope {\n",
" val innerScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)\n",
" innerScope.launch { work() } // NOT a child of outer\n",
"} // outer can finish while inner still runs\n",
"\n",
"runBlocking { outer() }"
],
"id": "7f8585ea7ebe7c99",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": "",
"id": "28611d6cd5448db3",
"outputs": [],
"execution_count": null
}
],
"metadata": {
"kernelspec": {
"display_name": "Kotlin",
"language": "kotlin",
"name": "kotlin"
},
"language_info": {
"name": "kotlin",
"version": "2.4.0-dev-2124",
"mimetype": "text/x-kotlin",
"file_extension": ".kt",
"pygments_lexer": "kotlin",
"codemirror_mode": "text/x-kotlin",
"nbconvert_exporter": ""
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment