Rails 3 Ractor : a parallel execution feature of Ruby without thread-safety concerns

Hi, This is Shubham Mishra from India,  this is the part of Ruby on Rails exploration journey.  In this post we will discuss about the ruby's some special concepts and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started...

Ruby 3 introduces Ractors, commonly referred to as Ruby Actors, as a new concurrency feature. They enable us to execute several threads of code concurrently without having to share memory or state. As a result, concurrent and parallel programming is made simpler, which can enhance performance and scalability.

It's necessary to grasp the fundamentals of concurrency and parallelism in programming before we delve into the specifics of Ractors. Concurrency is the capacity of a programme to manage many activities or execution threads concurrently. The term "parallelism" describes a program's capacity to run numerous processes or threads concurrently while utilising multiple processors or cores.

Traditionally, Ruby has used threads to provide concurrency; however, Ruby threads are implemented as green threads, which implies that the Ruby interpreter instead of the operating system schedules them. This can hinder their functionality and scalability, particularly when working with CPU- or I/O-intensive tasks.

Ractors provide a lightweight and isolated concurrency mechanism to overcome these limitations. Actors in other programming languages like Erlang or Akka are comparable to Ractors. They offer a method for establishing numerous separate, independent execution contexts, each with its own memory and state.

Creating a Ractor is easy. Here's an example:


In this example, we create a new Ractor and pass a block of code to execute in the Ractor context. We then call the take method on the Ractor object, which waits for the Ractor to complete its work and returns its result.

Ractors communicate with each other using messages. Here's an example:


In this example, we create two Ractors, ractor1 and ractor2. ractor2 sends a message to ractor1 using the send method. ractor1 receives the message using the receive method and prints it to the console.

Ractors can also be used to parallelize CPU-bound tasks. Here's an example:


In this example, we define a recursive Fibonacci function and parallelize it using Ractors. We create two Ractors, each computing a smaller Fibonacci number, and then add their results together.

Ractors are a strong and versatile feature in Ruby 3 that can aid in producing more streamlined, scalable, and enduring code. They connect with existing code and are comparatively simple to use. I urge you to give them a try if you haven't already!

You can explore my other blogs too here,
Get to know answers for common search on Google : A blog for posts which can help you for daily life problems, such as where to get free images, Topic suggestion for the blog.

Computer Science algorithms and other knowledge share : A blog for posts such as best search algorithm, Top interview questions for diffrent technologies, knowledge share for some frameworks or programming languages for the interview or in general terms.

My ideas to solve real world problems : A blog where me shared and presented my ideas to solve a real world problems, this will be interesting for me.

Future of computer science technology discussed : A blog where me discussed about the future of computer science and new technologies which will change our way for looking to solve problems.

Ruby on Rails Web development Blog : As the name suggest, it is the blog for sharing few knowledge about RoR web development framework.

Comments

Popular posts from this blog

Rails Postgresql Query timeout set and skip during migration

Rails Sync Postgresql and Elasticsearch Database of a Model

Ruby on Rails configure CORS setting with rack cors gem