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

Support for blocking calls inside coroutines (#79) #83

Closed
wants to merge 1 commit into from

Conversation

fvasco
Copy link
Contributor

@fvasco fvasco commented Jul 12, 2017

Added blocking and blockingAsync suspending functions for blocking code invocation inside a coroutine.

@fvasco fvasco force-pushed the 79-blocking-calls branch 2 times, most recently from 7ca7ffc to f5acb73 Compare July 14, 2017 04:27
@fvasco fvasco changed the base branch from master to develop July 14, 2017 19:35
@fvasco fvasco force-pushed the 79-blocking-calls branch from f5acb73 to e9e6613 Compare July 14, 2017 20:31
@elizarov elizarov force-pushed the develop branch 5 times, most recently from 0b13569 to 35d2c34 Compare July 20, 2017 16:36
start: CoroutineStart = CoroutineStart.DEFAULT,
block: () -> T): Deferred<T> {
require(start != CoroutineStart.UNDISPATCHED) { "Start blocking code undispatched is not supported" }
return async(executor.asCoroutineDispatcher(), start) { block() }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should cache dispatcher created from DefaultBlockingExecutor to avoid dispatcher creation on each call of the function and it would be helpful to have DefaultBlockingExecutor dispatcher (BlockingPool?) available as object same way as we have CommonPool.

Also, I'm not sure that it's good, that we don't allow to pass custom context here because context can be used not only for dispatchers.

* @param executor the executor for blocking code
* @param block the blocking code
*/
suspend fun <T> blocking(executor: Executor = DefaultBlockingExecutor, block: () -> T) =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think blocking should be covered by tests as well.

@fvasco
Copy link
Contributor Author

fvasco commented Sep 14, 2017

Hi @gildor,
I agree with you.

I wish receive some feedbacks about the future of this patch before continue the work.

@elizarov
Copy link
Contributor

elizarov commented Sep 18, 2017

I'm sorry for dragging my feet for so long here. I did not saw it as improvement versus run(myContext) { ... } for blocking calls and was thinking that we'd just provide an IO thread pool to solve it as explained in #79, which I did not like either, since providing too many contexts and switching back and forth between them is not really a user-friendly idea, until it clicked how we can have blocking and a single scheduler without any switching.

@fvasco fvasco closed this Sep 20, 2017
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

Successfully merging this pull request may close these issues.

3 participants