Grimbox
Grimbox

Reputation: 223

How to create async for loops?

Is it possible to make a for loop divided asynchronous? That each iteration will be calculated in parallel to speed up the iteration?

Upvotes: 0

Views: 720

Answers (1)

ndnenkov
ndnenkov

Reputation: 36110

You can use the parallel gem for that:

Parallel.each(elements) { |element| do_something(element) }

You can also control the number of threads (with the in_threads: keyword). If you need true parallelism, you can also specify the number of processes (with the in_processes: keyword).

Upvotes: 1

Related Questions