Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coroutines support. Non Blocking? #21

Open
Ulop opened this issue Aug 31, 2017 · 2 comments
Open

Coroutines support. Non Blocking? #21

Ulop opened this issue Aug 31, 2017 · 2 comments

Comments

@Ulop
Copy link

Ulop commented Aug 31, 2017

What do you think about using/support Kotlin? Would it give us async operations?
I made a test project with AsyncRoute implementation. It's only scratch for testing.

class AsyncRoute(path: String, val handler: suspend RouteHandler.() -> Any) : RouteImpl(path) {
    override fun handle(request: Request, response: Response): Any = runBlocking {
        kotlin.run {
            handler(RouteHandler(request, response))
        }
    }
}

and use it like this

http.service.addRoute(HttpMethod.get, AsyncRoute("/nonblocking") {
        val jobs = (0..10).map {
            async(CommonPool) {
                longComputationAwait(it)
            }
        }
        val total =  jobs.sumBy { it.await() }
        total.toString()
    })

where longComputationAwait is

suspend fun longComputationAwait(x: Int): Int {
    println("Compute started in: ${System.currentTimeMillis()}")
    delay(1000)
    return x * x
}

and blocking analog of this route

http.get("/blocking") {
        val total = List(10){ longComputation(it) }.sum()
        total.toString()
    }

where longComputation is

fun longComputation(x: Int): Int {
            Thread.sleep(1000)
            return x * x
        }

as result "/blocking" page recived respons in 10 seconds as expected
"/nonblocking" page recived it in 1 second recived with same total value

Have this idea reason for live? Can we get more complex coroutine support?

@perwendel
Copy link
Owner

Async is a requested feature in the java project as well. We'll try to add it there and expose it in kotlin framework as well!

@mgenov
Copy link

mgenov commented Feb 6, 2020

Are there any plans for this feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants