# Threads

**URL:** <https://metaruby.com/t/threads/444>\
**Category:** Ruby Chat\
**Created:** [February 21, 2016, 11:59am UTC](https://metaruby.com/t/threads/444 "2016-02-21T11:59:39Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [February 21, 2016, 11:59am UTC](https://metaruby.com/t/threads/444/1 "2016-02-21T11:59:39Z")

</div>

I just updated my, and my R.R. teams, website [Trendercast](http://trendercast.r15.railsrumble.com/). I made it far more responsive with:

1. an OAuth sign-in process that won’t navigate away from your page (wont interrupt podcast playing)
2. a submission notification whenever new RSS feeds are entered (they take a while to gather and process)
3. Threaded RSS feed updates!!!

**This is my first time trying out threads and I’m very happy with it.**

I experimented in IRB like a good developer and found that calling the join method on a thread will have the “program” wait for the thread to finish before resuming. This is one feature I wanted to use in Rails … but guess what… using thread.join in Rails gridlocks the application. Thankfully I found a way around that. Here’s basically what the code looks like.

```ruby
module FeedGenerator
  def self.gen_feed(url, view_env)
    # fetch record for RSS feed
    feed = Feed.where(url: url).take(1).first
    
    # If the feed is older than 4 hours we check to see if a new podcast has been posted
    # but we don't want to delay the user's page loading so lets just start a thread in the
    # background to perform fetching and DB updating
    threads[url] = Thread.new { perform(url) } unless updated?(feed)

    # if no record exists
    if feed.nil?
      # have the current process wait until data is finished being retrieved
      sleep 3 while threads[url].alive?

      # Allow the garbage collector to remove dead threads
      threads[url] = nil 

      # We have to change the query so Rails won't use the previously cached result
      # Grab the newly made feed
      feed = Feed.where(url: url).where("created_at > ?", Time.now - 20.minutes).take(1).first
    end 

    FeedPresenter.new(feed, view_env)
  end 

  def self.threads
    @threads ||= {}
  end 

  def self.perform(url)
    # grab RSS feed data and save to DB
  end

  def self.updated?(feed)
    if feed
      if feed.updated_at.between?(Time.now.ago(4.hours), Time.now)
        return true
      end
    end
    false
  end
end

```

The server runs great and the experience is everything I had hoped it would be. My next step is to write a sleeper agent in JavaScript when there is an update to be checked for and dynamically insert it in the page once it’s available.

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [February 21, 2016, 1:40pm UTC](https://metaruby.com/t/threads/444/2 "2016-02-21T13:40:07Z")

</div>

I might need to consider other users starting a thread for the same url at the same time. Perhaps I should rewrite the one line to:

```ruby
# if feed is not updated and thread isn't alive (or exists)
if !updated?(feed) and !threads[url].try{|t| !t.alive? }
  threads[url] = Thread.new { perform(url) }
end

```

There are things I don’t know… like if there are separate Ruby processes for every user or not. That would change what I would need to look for in threading.

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [February 21, 2016, 3:25pm UTC](https://metaruby.com/t/threads/444/3 "2016-02-21T15:25:11Z")

</div>

This seems like a recent up-to-date article, any help to you?

> **[How to Use Ruby Threads: An Easy To Understand Tutorial](https://www.rubyguides.com/2015/07/ruby-threads/)**
>
> What is a thread in Ruby? Threads make your Ruby programs do multiple things at the same time. Things like: Reading multiple files Handling multiple web request Making multiple API connections As a result of using threads, you'll have a...

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [February 21, 2016, 3:47pm UTC](https://metaruby.com/t/threads/444/4 "2016-02-21T15:47:23Z")

</div>

Thanks Aston! It’s very informative. It doesn’t cover my curiosity in how Rails works. I have a general idea about Rack have a per route listener (thread)… but I’ve only imagined about how it might work.

There are many things I wonder about. Like in Minitest I would want to do things in a test file with just **describe** and **it** blocks. In Rails I’ve found tests to be very strict about what you can do based on what folder you’re in and a basic [need for inheritance](https://gist.github.com/danielpclark/4f0c9b42658fbe1cbde8) to add abilities.

There are “some” people out there who know these things and if they happen to come by and speak up I’m most delighted to learn. Otherwise I’d need to invest a lot of time studying source code. I’m not against that, I’ll get around to that when I need to. I’d rather have connections and dialog than be a loner who’s read the source to the whole world.

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [February 21, 2016, 3:55pm UTC](https://metaruby.com/t/threads/444/5 "2016-02-21T15:55:30Z")

</div>

Yeah I totally hear you… hopefully when we get the new look a few new people will look in 😃

Not sure if you are keeping a eye on the [Elixir Forum](http://elixirforum.com) but José Valim kindly created a section on the official Elixir lang site for us! I’m hoping the powers that be in Ruby world might do the same :003:

Here’s hoping anyway :lol:

(Sorry to go off-topic - please feel free to delete or move to a separate thread)

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [February 21, 2016, 5:55pm UTC](https://metaruby.com/t/threads/444/6 "2016-02-21T17:55:16Z")

</div>

If I understand you, some of those answers depend on how you deploy rails. Unicorn vs. puma vs. whatever.

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [February 22, 2016, 12:07am UTC](https://metaruby.com/t/threads/444/7 "2016-02-22T00:07:26Z")

</div>

@kofno Yeah, I do know Puma is thread oriented and Heroku is promoting it over Unicorn now.

On the topic of threads this showed up on my Twitter feed: [Ruby Threads and ActiveRecord Connections](http://jakeyesbeck.com/2016/02/14/ruby-threads-and-active-record-connections/) and it was written a week ago. Good insight on database connection/pooling with threading.

And from what I hear [Celluloid](https://celluloid.io/) is the best system for handling threads in Ruby (even better in JRuby) and Sidekiq is the best threaded background system which has become mature enough to graduate away from Celluloid and they’ve written it now with pure Ruby threads. That is pretty much the extent of my knowledge on threads.

That and Ruby’s “gil” is problematic for threads except with cases of IO.

---

<div class="post-metadata">

**Author:** ![tak1n](https://metaruby.com/user_avatar/metaruby.com/tak1n/32/862_2.png) [@tak1n](https://metaruby.com/u/tak1n)\
**Post date:** [February 22, 2016, 1:41am UTC](https://metaruby.com/t/threads/444/8 "2016-02-22T01:41:49Z")

</div>

@danielpclark you maybe also want to take a look on [GitHub - ruby-concurrency/concurrent-ruby: Modern concurrency tools including agents, futures, promises, thread pools, supervisors, and more. Inspired by Erlang, Clojure, Scala, Go, Java, JavaScript, and classic concurrency patterns.](https://github.com/ruby-concurrency/concurrent-ruby)  
Sidekiq [uses it](https://github.com/mperham/sidekiq/blob/master/sidekiq.gemspec#L20) now (In addition to bare bone threads) and also rails is [using it.](https://github.com/rails/rails/blob/c85a4f1f0f72f11af886714a724b965c38e4cdaf/activesupport/activesupport.gemspec#L26) 🙂

And I’m currently looking into it with a fun project: [GitHub - tak1n/reifier: A rack app server written in pure ruby](https://github.com/tak1n/reifier)

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [February 22, 2016, 1:49am UTC](https://metaruby.com/t/threads/444/9 "2016-02-22T01:49:25Z")

</div>

If you want to learn more about ruby threads, I can recommend this book: [http://www.jstorimer.com/products/working-with-ruby-threads](http://www.jstorimer.com/products/working-with-ruby-threads)

---

<div class="post-metadata">

**Author:** ![ludovic](https://metaruby.com/user_avatar/metaruby.com/ludovic/32/183_2.png) [@ludovic](https://metaruby.com/u/ludovic)\
**Post date:** [February 23, 2016, 1:55pm UTC](https://metaruby.com/t/threads/444/10 "2016-02-23T13:55:22Z")

</div>

Celluloid use Fibers to organize his “tasks” flollowing the actor model into a single thread. But you can also use multiple thread each with there own pool of fibers (drived by Celluloid, of course). Be careful with JRuby : Fibers are real threads on this platform and you can reach the limit of your system. For MRI, it’s not a problem because fibers are not system threads (and should not be).

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [March 18, 2016, 2:02pm UTC](https://metaruby.com/t/threads/444/11 "2016-03-18T14:02:39Z")

</div>

My threads are still working fine on the server. But they seem to have introduced an issue with DB access `ActiveRecord::ConnectionTimeoutError`. I need to somehow allow more access to the DB when threads start.

Spinning up a bunch of threads by checking all of the podcasts gets a bunch of threads reading for changes and writing updates. This can have some of the podcasts to seem to not load in the browser… probably the ones not in cache. 😕

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [March 20, 2016, 1:01pm UTC](https://metaruby.com/t/threads/444/12 "2016-03-20T13:01:52Z")

</div>

Have you introduced a connection pool?

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [March 20, 2016, 5:40pm UTC](https://metaruby.com/t/threads/444/13 "2016-03-20T17:40:35Z")

</div>

Not yet. I’ve been thinking about it because I remember seeing blog posts about them.

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [March 20, 2016, 6:52pm UTC](https://metaruby.com/t/threads/444/14 "2016-03-20T18:52:18Z")

</div>

I think I’ve got it!

```ruby
  # if feed is not updated and thread isn't alive (or exists)
  if !updated?(feed) and !threads[url].try{|t| !t.alive? }
    threads[url] = Thread.new { perform(url) }
  end

def perform(url)
  ActiveRecord::Base.connection_pool.with_connection do
    # ...
  end
end

```

I just wrapped the one place I was calling `Thread.new` with the `ActiveRecord::Base.connection_pool.with_connection` and the site no longer comes to a halt. 😄

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [March 21, 2016, 12:07pm UTC](https://metaruby.com/t/threads/444/15 "2016-03-21T12:07:05Z")

</div>

👏 🎊

Glad you figured it out!

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [March 23, 2016, 11:44pm UTC](https://metaruby.com/t/threads/444/16 "2016-03-23T23:44:26Z")

</div>

It turns out the Podcast feeds aren’t updating anymore. I think I might just write my own ActiveRecord thread pool with priority access.

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [May 11, 2016, 1:02am UTC](https://metaruby.com/t/threads/444/17 "2016-05-11T01:02:00Z")

</div>

> [@danielpclark](#):
>
> It turns out the Podcast feeds aren’t updating anymore. I think I might just write my own ActiveRecord thread pool with priority access.

I don’t know why but it’s working without issue now for months.
