Problem with Ruby Multithreading

N

Neha Gupta

Hello Guys,

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

(e-mail address removed)
 
C

Christopher Small

First of all, do you mean "multithreading" as in the context of
wikipedia's definition
(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.
 
C

Charles Oliver Nutter

Neha said:
Hello Guys,

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

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
 
C

Christopher Small

I received this private mail from Neha Gupta
Thanx for your reply.
By finders i mean, I am unable to use "<Modelname>.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

http://railscasts.com/episodes/74

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top