<@U0BFDUP0E> A coroutines threading model is diffe...
# coroutines
@jw A coroutines threading model is different from Rx threading model. In Rx the thread your code runs in is implicitly controlled by various onXXX operators up/down the stream, while in coroutines the thread (context) is explicitly specified for each coroutine. The only exception is
context, which leaves the thread “unspecified”. 4 replies
context gives you similar, but not really the same behavior as Rx. Consider the following pipeline:
Copy code
produce(Context1) { ... }.map(Unconfined) { ... }.consume(Context2) { ... }
So, you have a producer running in one thread and a consumer in another thread, with unconfined transformer in between. What thread the transformer runs in? The answer — it depends. With standard channel implementations from
the transformer sometimes runs in one thread, sometimes in the other. If the producer is faster than the consumer, then the producer is resuming suspended transformer coroutine, thus the transformer runs in the first thread. However, if the consumer is faster than the producer, then the consumer is resuming suspended transformer coroutine and thus transformer runs in the second thread. In order to imitate Rx behavior you either have to explicitly inherit producer’s context or exercise special care about the kinds of channels you are using in between the coroutines.