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

Discussion in 'Ruby' started by estebanjang@gmail.com, Nov 6, 2007.

  1. Guest

    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
    , Nov 6, 2007
    #1
    1. Advertising

  2. Szymon Rozga Guest

    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

    On Nov 6, 3:26 pm, wrote:
    > 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
    Szymon Rozga, Nov 6, 2007
    #2
    1. Advertising

  3. > 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
    Jones, Brian - McClatchy Interactive, Nov 6, 2007
    #3
  4. Re: Long running SQL query via ActiveRecord blocking the entire Rubyprocess?

    On 11/6/07, <> wrote:
    > 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
    Jeremy Kemper, Nov 6, 2007
    #4
  5. snacktime Guest

    Re: Long running SQL query via ActiveRecord blocking the entire Rubyprocess?

    On Nov 6, 2007 12:30 PM, <> wrote:
    >
    > 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
    snacktime, Nov 6, 2007
    #5
  6. ara.t.howard Guest

    Re: Long running SQL query via ActiveRecord blocking the entire Rubyprocess?

    On Nov 6, 2007, at 2:47 PM, Jeremy Kemper wrote:

    > 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/
    --
    we can deny everything, except that we have the possibility of being
    better. simply reflect on that.
    h.h. the 14th dalai lama
    ara.t.howard, Nov 6, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonymous
    Replies:
    0
    Views:
    1,455
    Anonymous
    Oct 13, 2005
  2. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    672
    Eric Sosman
    Jul 8, 2003
  3. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,184
    Peter Shaggy Haywood
    Sep 20, 2005
  4. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    471
    santosh
    Jul 24, 2007
  5. Hannes Wyss
    Replies:
    0
    Views:
    254
    Hannes Wyss
    Jul 18, 2007
Loading...

Share This Page