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

Question: using async-scoped with for_each_concurrent #22

Closed
nyurik opened this issue Dec 27, 2023 · 2 comments
Closed

Question: using async-scoped with for_each_concurrent #22

nyurik opened this issue Dec 27, 2023 · 2 comments

Comments

@nyurik
Copy link

nyurik commented Dec 27, 2023

In my massively-parallel processing as part of MapLibre's Martin code I use for_each_concurrent to run hundreds of millions of tasks in parallel. I was trying to understand the point you were making in the docs about its inefficiencies, or if I am using it wrong, but I might have misunderstood. Is this the right approach, or should I go with some alternative that involves scoped async? Thanks!

@rmanoka
Copy link
Owner

rmanoka commented Dec 27, 2023

The signature of the "map" function in [for_each_concurrent] is F: FnMut<...>. This implies that the "map" function is never executed parallely. The futures in the stream are awaited concurrently, but not in parallel (the poll method is never run simultaneously in multiple CPU cores). Essentially, I/O, signals, etc. are awaited concurrently across the futures.

For parallelism, I've typically used rayon for CPU heavy algorithm parallelization. Another option is to use the spawn function of the executor (tokio/async-std/...), to spawn separate futures that are then run parallelly. Then, we simply await their join-results concurrently. In this case, the F is essentially no-op, so it's okay to not schedule it in parallel. Now, spawn expects 'static future, so this crate provides an ergo. API to spawn local futures (not 'static).

@rmanoka
Copy link
Owner

rmanoka commented Jan 26, 2024

Closing as resolved.

@rmanoka rmanoka closed this as completed Jan 26, 2024
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