-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Support thread interrupting blocking functions #1947
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
Comments
Great! |
This is implementation of issue Kotlin#1947 Signed-off-by: Trol <[email protected]>
This is implementation of issue Kotlin#1947 Signed-off-by: Trol <[email protected]>
The Considering the I put on the table: suspend fun runInterruptible(dispatcher: Dispatcher = Dispatcher.IO, block: () -> T) |
It is perfectly fine as I could see. I don't understand what the concern is here.
I don't think that is appropriate. If the current context is Dispatchers.Default, the function above makes running the blocking block in current context impossible without knowing what the current context is and explicitly put it on the argument of |
Hi @jxdabc, The More in general, I expect that a So, if the target of a
I didn't understand your point, sorry. |
@fvasco
|
Hi @jxdabc, I rewrote my questions. These are personal considerations. Should Should I put an idea on the table, it is not a proposal. In my opinion, Please consider this code:
In such case
So we have to understand what is the use-case for I pose these questions because the Finally, should |
@fvasco I think I caught your point : )
The fact is, as implemented currently, |
Here is our final design of the API:
|
This is implementation of issue Kotlin#1947 Signed-off-by: Trol <[email protected]>
* Support thread interrupting blocking functions (#1947) This is the implementation of issue #1947 Signed-off-by: Trol <[email protected]> Co-authored-by: Trol <[email protected]>
Hi, thanks for implementing this! It will be very helpful when using blocking IO in coroutines.
This has been released in |
* Support thread interrupting blocking functions (Kotlin#1947) This is the implementation of issue Kotlin#1947 Signed-off-by: Trol <[email protected]> Co-authored-by: Trol <[email protected]>
This idea supersedes the idea that was originally presented in #57 (see discussion and use-cases there). Here is the direct link to the comment by @jxdabc with this proposal: #57 (comment)
The idea to create a wrapper function that converts a regular blocking code in a suspending cancellable function that converts cancellation into interruption of the underlying blocking primitive:
We might want to tweak naming a bit, though. Here are the options that I see on the table drawing from the traditions in our naming conventions. Additional ideas are welcome. In these examples I show them used to wrap an interruptible
queue.take()
call from JDK:interruptible { queue.take() }
runInterruptible { queue.take() }
withInterrupt ( queue.take() }
interruptOnCancel { queue.take() }
runInterruptingOnCancel { queue.take() }
withInteruptOnCancel { queue.take() }
We should also consider an optional (or maybe even a required?) context parameter to this function. The reason for this is that it will enable single-call conversion of interruptible Java methods into main-safe suspending functions like this:
The text was updated successfully, but these errors were encountered: