Created
October 18, 2020 15:55
-
-
Save hrom512/fd0f6c9e8ec3dd040a5a337ca2f31607 to your computer and use it in GitHub Desktop.
Ruby 3.0 Ractor example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| scheduler = | |
| Ractor.new do | |
| loop do | |
| Ractor.yield Ractor.recv | |
| end | |
| end | |
| workers = | |
| 8.times.map do |i| | |
| Ractor.new(scheduler, Ractor.current) do |r_input, r_output| | |
| while inp = r_input.take | |
| r_output << [inp, inp ** inp] | |
| end | |
| end | |
| end | |
| TASKS = 30_000 | |
| (1..TASKS).each do |i| | |
| scheduler << i | |
| end | |
| completed = 0 | |
| while completed < TASKS && res = Ractor.recv | |
| p res.first if res.first % 1000 == 0 | |
| completed += 1 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment