It is a continuation of previous discussion here, but I decided to open new topic.
The question is about the best way to use lazy detached coroutines. I mean the case,when coroutine is created (without starting) in one place and then called in another place. I use it to create lazy task graphs, but it would be useful for other future usages like serializable coroutines and distributed computing.
Current problem is that if I create a lazy
Deferred in a scope and then call it outside of it, it won’t work since parent scope is closed. Basically, detached coroutines violate structured concurrency. There are two solutions:
- Use GlobalScope or some other scope that is guaranteed to span over both creation site and use site. It seems not the best solution, since we completely miss all structured concurrency benefits. Also, if we consider serialized coroutines, it won’t help since they are produced outside of even large scope.
- We can bundle a suspendend funcition with coroutine context and transform it to actual
Deferredon call site using provided scope. It seems like a solution, but requires some additional work.
Any thoughts about it?