Skip to content

WeakJob #2626

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

Closed
InsanusMokrassar opened this issue Apr 5, 2021 · 5 comments
Closed

WeakJob #2626

InsanusMokrassar opened this issue Apr 5, 2021 · 5 comments

Comments

@InsanusMokrassar
Copy link

I have a feature request about WeakJobs. Currently in my practice there are tasks (Job) which are not important in context of CoroutinesScope and can be (sometimes must be) cancelled when there are no any strong Job (common Job) left in this CoroutineScope. As I remember, currently when we are calling launch/async methods on CoroutineScope we will get new Job and this Job will prohibit CoroutineScope to be completed until Job will not be completed too.

Example of syntax

val scope = CoroutineScope(Dispatchers.Default)
scope.weakLaunch {
    while (isActive) {
        println("still working")
    }
}
scope.launch {
    delay(5100L)
    println("that will be last one")
}

It is expected that the code above will print:

still working
still working
still working
still working
still working
that will be last one
@InsanusMokrassar
Copy link
Author

Currently there is way to get the same functionality (as I understand):

val scope = CoroutineScope(Dispatchers.Default)
val subScope = CoroutineScope(scope.coroutineContext)
scope.coroutineContext.job.invokeOnCompletion { subScope.cancel() }

subScope.launch {
    while (isActive) {
        println("still working")
    }
}
scope.launch {
    delay(5100L)
    println("that will be last one")
}

@MairwunNx
Copy link

The name weak slightly violates the principle of naming objects that are weak to the garbage collector. Maybe you should choose a different name for such a function?

@InsanusMokrassar
Copy link
Author

InsanusMokrassar commented Apr 5, 2021

The name weak slightly violates the principle of naming objects that are weak to the garbage collector. Maybe you should choose a different name for such a function?

I have suggested this name exactly because of weak this scope is not direct child and should not be counted as subscope for its parent

@fvasco
Copy link
Contributor

fvasco commented Apr 5, 2021

I wrote something similar here: #1065

@elizarov
Copy link
Contributor

elizarov commented Apr 6, 2021

@fvasco Thanks for the link. Yes, this request is essentially a duplicate of #1065

@elizarov elizarov closed this as completed Apr 6, 2021
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

4 participants