WIP: Ractors #25
							
								
								
									
										51
									
								
								lib/tasks/ractors.rake
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								lib/tasks/ractors.rake
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,51 @@
 | 
				
			|||||||
 | 
					namespace :ractors do
 | 
				
			||||||
 | 
					  desc 'Run the Ractors example'
 | 
				
			||||||
 | 
					  task run: :environment do
 | 
				
			||||||
 | 
					    list = (1..20)
 | 
				
			||||||
 | 
					    number_of_ractors = Concurrent.processor_count
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ractors = number_of_ractors.times.map do |i|
 | 
				
			||||||
 | 
					      Ractor.new(name: "Ractor #{i}") do
 | 
				
			||||||
 | 
					        loop do
 | 
				
			||||||
 | 
					          number = Ractor.receive
 | 
				
			||||||
 | 
					          puts "Processing #{number} in #{Ractor.current.name}"
 | 
				
			||||||
 | 
					          Ractor.yield "eureka" if number == 17
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end.cycle
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    list.each do |i|
 | 
				
			||||||
 | 
					      ractors.next.send(i)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    puts "all enqueued"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    binding.pry
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Ractor.select(*ractors)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # binding.pry
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # number_of_ractors.times do |i|
 | 
				
			||||||
 | 
					    #   r = Ractor.new(name: "Ractor #{i}") do
 | 
				
			||||||
 | 
					    #     Ractor.receive.each do |i|
 | 
				
			||||||
 | 
					    #       puts "Processing index #{i} in #{Ractor.current.name}"
 | 
				
			||||||
 | 
					    #     end
 | 
				
			||||||
 | 
					    #   end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #   r.send((i * 250)...((i + 1) * 250))
 | 
				
			||||||
 | 
					    # end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # ractor = Ractor.new do
 | 
				
			||||||
 | 
					    #   loop do
 | 
				
			||||||
 | 
					    #     puts 'I will receive a message soon'
 | 
				
			||||||
 | 
					    #     msg = Ractor.receive
 | 
				
			||||||
 | 
					    #     puts 'I will return a tripple of what I receive'
 | 
				
			||||||
 | 
					    #     Ractor.yield(msg * 3)
 | 
				
			||||||
 | 
					    #   end
 | 
				
			||||||
 | 
					    # end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # heavy_calculation.send(15)
 | 
				
			||||||
 | 
					    # puts heavy_calculation.take
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user