Long running SQL query via ActiveRecord blocking the entire Ruby process?

E

estebanjang

To my surprise, a long running SQL query via ActiveRecord blocks an
entire Ruby process (all the other threads are blocked while the
thread that runs the SQL query is waiting for the result set)

Has anyone else found this to be the case? (am I mistaken?)
Is there any way to avoid this other than spawning a separate process?

[Ruby version 1.8.4, ActiveRecord version 1.14.4 on Linux]

-Steve
 
S

Szymon Rozga

It is my understanding that Ruby threads are green threads.

see: http://en.wikipedia.org/wiki/Green_threads
"Also a green thread may block all other threads if performing a
blocking I/O operation. In order to avoid these problems, green
threads must use asynchronous I/O operations, but the added complexity
increases latency."

I seem to remember reading about Ruby threads and IO blocking, but
cannot find it.

Hope the above answers some questions.

Does anyone know of a solution?

-Szymon
 
J

Jones, Brian - McClatchy Interactive

To my surprise, a long running SQL query via ActiveRecord=20
blocks an entire Ruby process (all the other threads are=20
blocked while the thread that runs the SQL query is waiting=20
for the result set)
=20
Has anyone else found this to be the case? (am I mistaken?)=20
Is there any way to avoid this other than spawning a separate process?
=20
[Ruby version 1.8.4, ActiveRecord version 1.14.4 on Linux]

As far as I know, things like Mongrel obtain a global lock -> run the
request through rails -> and release the lock. Rails isn't thread safe,
and a great deal of hackery depends upon this non-threaded mode of
operation. To handle multiple requests, you'll need multiple processes.


I'm curious, do you have something attempting to run the same rails in
multiple ruby threads at the same time?

Brian
 
J

Jeremy Kemper

To my surprise, a long running SQL query via ActiveRecord blocks an
entire Ruby process (all the other threads are blocked while the
thread that runs the SQL query is waiting for the result set)

Has anyone else found this to be the case? (am I mistaken?)
Is there any way to avoid this other than spawning a separate process?

The postgres and oracle adapters support async queries which won't
block Ruby threads, or you can use any of the pure-Ruby database
drivers.

Forking processes is probably better though.

jeremy
 
S

snacktime

To my surprise, a long running SQL query via ActiveRecord blocks an
entire Ruby process (all the other threads are blocked while the
thread that runs the SQL query is waiting for the result set)

Has anyone else found this to be the case? (am I mistaken?)
Is there any way to avoid this other than spawning a separate process?

C extensions that block will block the whole process, as they
basically interrupt ruby's thread schedular while they are running.
Unless the extension is written with this in mind, which most are not.

Chris
 
A

ara.t.howard

The postgres and oracle adapters support async queries which won't
block Ruby threads, or you can use any of the pure-Ruby database
drivers.

Forking processes is probably better though.

i'll be releasing a background job manager for rails in a few
days... fyi.

a @ http://codeforpeople.com/
 

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
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top