# Problem with Ruby Multithreading

**URL:** <https://www.ruby-forum.com/t/problem-with-ruby-multithreading/141867>\
**Category:** Ruby\
**Created:** [July 1, 2008, 9:00am UTC](https://www.ruby-forum.com/t/problem-with-ruby-multithreading/141867 "2008-07-01T09:00:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neha\_Gupta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bbe5ce/32.png) [@Neha\_Gupta](https://www.ruby-forum.com/u/Neha_Gupta)\
**Post date:** [July 1, 2008, 9:00am UTC](https://www.ruby-forum.com/t/problem-with-ruby-multithreading/141867/1 "2008-07-01T09:00:58Z")

</div>

Hello G.s,

I am trying to use threads in my RoR app.  
But unfortunately I came across a very strange issue.  
I am unable to use “finders” within my thread to read data from my  
database.  
In addition to this, the Thread does not allow me to create any new  
object inside the Thread.

Eg:

class TagsController \< ApplicationController

def some\_method

begin  
Thread.new do  
new\_user = User.new  
end  
rescue  
p $!  
end

end

end

Throws an exception saying  
“#\<ArgumentError: A copy of TagsController has been removed from the  
module tree but is still active!\>”

Could somebody guide me where am I going wrong.

Thanks & Regards,

Neha Gupta

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Neha\_Gupta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bbe5ce/32.png) [@Neha\_Gupta](https://www.ruby-forum.com/u/Neha_Gupta)\
**Post date:** [July 1, 2008, 10:21am UTC](https://www.ruby-forum.com/t/problem-with-ruby-multithreading/141867/2 "2008-07-01T10:21:57Z")

</div>

First of all, do you mean “multithreading” as in the context of  
wikipedia’s definition  
([Thread (computing) - Wikipedia](http://en.wikipedia.org/wiki/Thread_(computer_science))), because if so,  
I don’t see the connection. Otherwise, I imagine that you are talking  
about forums with multiple threads, or something to that effect.

> I am trying to use threads in my RoR app.  
> But unfortunately I came across a very strange issue.  
> I am unable to use “finders” within my thread to read data from my  
> database.

Are “finders” a specific sort of object in RoR? If so I know nothing  
about them. If you are talking about find methods to search through  
database entries, that’s another thing - is this what you mean?

> In addition to this, the Thread does not allow me to create any new  
> object inside the Thread.
> 
> Eg:
> 
> class TagsController \< ApplicationController
> 
> def some\_method
> 
> begin  
> Thread.new do  
> new\_user = User.new  
> end  
> rescue  
> p $!  
> end
> 
> end
> 
> end
> 
> Throws an exception saying  
> “#\<ArgumentError: A copy of TagsController has been removed from the  
> module tree but is still active!\>”

First of all, are you writing your app RESTfully? What is “some\_method”?  
Also, I’m not quite clear about what you are trying to do here - is it  
create a new thread and a new user at the same time?

I hope that I can be of some help.

---

<div class="post-metadata">

**Author:** ![Neha\_Gupta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bbe5ce/32.png) [@Neha\_Gupta](https://www.ruby-forum.com/u/Neha_Gupta)\
**Post date:** [July 1, 2008, 3:02pm UTC](https://www.ruby-forum.com/t/problem-with-ruby-multithreading/141867/3 "2008-07-01T15:02:42Z")

</div>

Neha Gupta wrote:

> end
> 
> end
> 
> end

Like others, I’m not sure what you’re trying to accomplish, but any  
exception thrown by the code in that thread will never escape the  
thread, and so this rescue is mostly useless. What you’re probably  
seeing is that the TagsController has been reloaded in development mode  
before your thread actually runs. Since your thread holds on to a  
reference to the original TagsController, you’ve got multiple versions  
of it active at once, which Rails is warning you about.

Is your intention to hit the database asynchronously? You should  
probably know that ActiveRecord isn’t the most thread-safe thing in the  
world yet, so you’re probably going to run into trouble.

- Charlie

---

<div class="post-metadata">

**Author:** ![Neha\_Gupta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bbe5ce/32.png) [@Neha\_Gupta](https://www.ruby-forum.com/u/Neha_Gupta)\
**Post date:** [July 1, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/problem-with-ruby-multithreading/141867/4 "2008-07-01T20:08:20Z")

</div>

I received this private mail from Neha Gupta

> Thanx for your reply.  
> By finders i mean, I am unable to use “.find” methods in my  
> thread.  
> And some\_method was just a temporary name I used in my example. Actual  
> method name is create.  
> I am trying to create a new thread and do few database insert  
> operations in the thread.  
> Is that possible to do in RoR ?

I’m not sure why you are having issues with the find method, but if you  
provided some code it might be easier to troubleshoot.

There is a rails way to create multiple objects with 1 call to create.  
You might want to take a look at the following screencasts - they helped  
me when I was in a similar situation to yours

> **[\#74 Complex Forms Part 2 - RailsCasts](http://railscasts.com/episodes/74-complex-forms-part-2)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

They are very good screencasts, and although they might not be tailored  
to exactly your situation, they should help you along your way. I think  
you are probably going to want to start inside of your new definition  
within the controller to build the users from within that method call.  
Then you need (depending on how you want to work things) to figure out  
how to handle things in your views (with forms and what not - you’ll  
probably have to use fields\_for in addition to form\_for) and how you do  
that determines how you will write your create method to get things  
working. If you post back on this forum with more specific details of  
what exactly you are looking to do, someone can give more help. The  
screencasts will likely get you far though (methinks).

Cheers, and good luck!
