Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 443439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:10:16+00:00 2026-05-12T21:10:16+00:00

I am trying to run multiple tasks, each task access the database, and I

  • 0

I am trying to run multiple tasks, each task access the database, and I am trying to run the tasks into separate execution wires.

I played around, tried allow_concurrency which I have set to true, or config.thread_safe! but it I get un-deterministic errors, for example sometimes a class is missing, or a constant …

here is some code

grabbers = get_grabber_name_list
threads = []
grabbers.each { |grabber|
  threads << Thread.new {
    ARGV[0] = grabber
    if (@@last_run_timestamp[grabber.to_sym].blank? || (@@last_run_timestamp[grabber.to_sym] >= AbstractGrabber.aff_net_accounts(grabber, "grab_interval").seconds.ago))
    Rake::Task["aff_net:import:" + grabber].execute
    @@last_run_timestamp.merge!({grabber.to_sym => Time.now})
  end
  }
}
threads.each {|t| t.join }

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:10:16+00:00Added an answer on May 12, 2026 at 9:10 pm

    I’ve recently implemented a Rails application that uses threads and made a few discoveries:

    First, if you’re writing to any arrays or hashes (i.e., complex types) outside your thread, wrap them in a mutex. It looks to me like hash and array references may not be thread safe. It seems unlikely that hash/array element indexing isn’t thread safe but all I know is that after I put the external data structures in a mutex before writing, problems disappeared.

    Second, close your ActiveRecord connection when the thread terminates, otherwise you can end up creating a large number of stale connections. Here’s a post about how to do this. I don’t know if it still applies for Rails versions > 2.2 but after I started closing connections explicitly, my problems disappeared. The author suggests monkey-patching ActiveRecord to do this automatically but I decided to release connections explicitly in my code.

    Here’s a sample of code that’s working for me:

    mutex = Mutex.new
    my_array = []
    threads = []
    1.upto(10) do |i|
      threads << Thread.new {
         begin
           do_some_stuff
           mutex.synchronize {
             # You'd think that each thread would only touch its own personal
             # array element but without a mutex, I run into problems.
             my_array[i] = some_computed_value
           }
         ensure
           ActiveRecord::Base.connection_pool.release_connection
         end
       }
    }
    threads.each {|t| t.join}
    

    By the way, if you’re using threads to take advantage of multi-core CPUs, you’ll need to use JRuby. As far as I know, JRuby is the only implementation that can take advantage of native CPU threads. If you use threads so you can do other things while waiting on network connections or some other non-CPU task, this isn’t an issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 223k
  • Answers 223k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You cannot determine the height until it's rendered. So with… May 13, 2026 at 12:39 am
  • Editorial Team
    Editorial Team added an answer if you want it to fit in $sortby = $_GET['sortby'];… May 13, 2026 at 12:39 am
  • Editorial Team
    Editorial Team added an answer You've forgotten to put in WHERE clause: UPDATE account SET… May 13, 2026 at 12:39 am

Related Questions

I am trying to learn Rails by working with different packages (ActiveRecord, ActiveSupport) without
I use eclipse for quite a lot of work, including: multiple utility projects that
Using Java 6: I have a method that uses a Thread to run a
I am trying to find out if it is possible to configure One instance

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.