Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Thread.new in Rails action

When using a multi-process app server and running Thread.new (spawning multiple threads) in a controller action, is parallelism attained? Or does the GIL prevent this from happening?

like image 760
mstrom Avatar asked Oct 28 '25 23:10

mstrom


1 Answers

The Global Interpreter Lock (GIL) prevents more than one Ruby thread from running at any given time, which has the net effect of restricting Ruby to one core only. Other than that, they are actual threads.

If you need true concurrency you need to run a Ruby without a GIL, like JRuby.

That being said, threads in Ruby are fine for many operations where you're not pinning a CPU core. If you are you should probably create background jobs for those operations through ActiveJob. These can run in one or more processes on different systems if necessary.

like image 92
tadman Avatar answered Oct 30 '25 13:10

tadman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!