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

Multiple, parallel Channel consumers #441

Closed
grodzickir opened this issue Jul 19, 2018 · 1 comment
Closed

Multiple, parallel Channel consumers #441

grodzickir opened this issue Jul 19, 2018 · 1 comment

Comments

@grodzickir
Copy link

Hello,
I have a problem and an enhancement proposition.
I'm trying to use a Channel to fetch data from web and simultaneously insert it into db in parallel.
Fetching data is much faster than inserting, as I have to check if the data changed etc.

Here I have a problem how to properly implement a single producer and multiple parallel consumers of a single Channel.
Is there any better way than writing:


    val jobList: ArrayList<Job> = ArrayList()
    (1..20).forEach {
        jobList += launch(coroutineContext) {
            val x = myChannel.receiveOrNull()
            if(x != null) {
                //process
            }
        }
    }
    jobList.forEach {
        it.join()
    }

?

Assuming that I launched it in another coroutine with newFixedThreadPoolContext(20, "name") I would get a nice, parallel processing of the Channel.

Using this approach I can't use functions like map or filter without some weird boxing of one value into a collection. Thus it would be really appreciated to be able to do sth like :


    channel.parallel(coroutineContext/newFixedThreadPoolContext())
                .map{ ... }
                .filter{....}
                .consumeEach{...}
@elizarov
Copy link
Contributor

This seems to be a duplicate of #172. Let's continue discussion there.

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

2 participants