@mhmtszr (Mehmet Sezer) posted a new topic, subject "Kotlin SDK every requests block the current thread". It read:

   Hello,
    
    We used to use Reactive Java SDK for our Kotlin applications to gain non-blocking io operations(we made some operations on it to support native Kotlin coroutines). In Kotlin SDK as I see, there are no operations like “suspendCoroutine” or “suspendCancellableCoroutine” to prevent the thread from being idle.  
    If every operation suspends the current coroutine(release the current thread) and continues when the io operation is done, It would be great for us to scale perspective.
    
    Thanks.

To which @david.nault (David Nault) replied

    Hi Mehmet,
    
    Welcome to the forum!
    
    > If every operation suspends the current coroutine(release the current thread) and continues when the io operation is done, It would be great for us to scale perspective.
    
    The methods in the Kotlin SDK that do IO are all `suspend` functions, and do indeed release the current thread until the operation completes.
    
    The Kotlin SDK uses the same asynchronous core library as the Java SDK. The core library uses Reactor for efficient async IO. The Kotlin SDK uses `kotlinx-coroutines-jdk8` and `kotlinx-coroutines-reactive` to suspend the current coroutine while the IO completes.
    
    For example, let's look at this code for executing a KV request. The core library starts doing the IO asynchronously when `send(request)` is called. The `request.response()` method returns a `CompletableFuture` that completes when the IO is done. The `CompletableFuture.await()` extension function from `kotlinx-coroutines-jdk8` suspends the current coroutine until the IO is complete and the response is ready.
    
    tldr; The Kotlin SDK doesn't call `suspendCoroutine` or `suspendCancellableCoroutine` because the core library is already asynchronous.
    
    Thanks,  
    David

After which, @mhmtszr (Mehmet Sezer) added

    Hello David,
    
    Thank you very much for your detailed explanation. I totally missed the **await** method in library code, as you said it also suspends the current coroutine.
    
    Thanks.


{'source': 'discourse', 'url': 'https://forums.couchbase.com/t/kotlin-sdk-every-requests-block-the-current-thread/32949', 'title': 'Kotlin SDK every requests block the current thread', 'views': 528, 'has_accepted_answer': True, 'word_count': 311, 'like_count': 0, 'reply_count': 1, 'participant_count': 2, 'posts': 3, 'tags': '', 'author': '@mhmtszr (Mehmet Sezer)', 'participants': '@mhmtszr (Mehmet Sezer), @david.nault (David Nault)', 'image_url': [None], 'created_at': '2022-02-27T17:06:14.269Z', 'last_posted_at': '2022-02-28T05:51:47.595Z'}