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

  • SEARCH
  • Home
  • 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 8659179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:56:58+00:00 2026-06-12T15:56:58+00:00

I’m writing a rake task that would be called every minute (possibly every 30

  • 0

I’m writing a rake task that would be called every minute (possibly every 30 seconds in the future) by Whenever, and it contacts a polling API endpoint (per user in our database). Obviously, this is not efficient run as a single thread, but is it possible to multithread? If not, is there a good event-based HTTP library that would be able to get the job done?

  • 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-06-12T15:56:59+00:00Added an answer on June 12, 2026 at 3:56 pm

    I’m writing a rake task that would be called every minute (possibly every 30 seconds in the future) by Whenever

    Beware of Rails startup times, it might be better to use a forking model such as Resque or Sidekiq, Rescue provides https://github.com/bvandenbos/resque-scheduler which should be able to do what you need, I can’t speak about Sidekiq, but I’m sure it has something similar available (Sidekiq is much newer than Resque)

    Obviously, this is not efficient run as a single thread, but is it possible to multithread? If not, is there a good event-based HTTP library that would be able to get the job done?

    I’d suggest you look at ActiveRecord’s find_each for tips on making your finder process more efficient, once you have your batches you can easily do something using threads such as:

    #
    # Find each returns 50 by default, you can pass options
    # to optimize that for larger (or smaller) batch sizes
    # depending on your available RAM
    #
    Users.find_each do |batch_of_users|
      #
      # Find each returns an Enumerable collection of users
      # in that batch, they'll be always smaller than or 
      # equal to the batch size chosen in `find_each`
      #
      #
      # We collect a bunch of new threads, one for each
      # user, eac 
      #
      batch_threads = batch_of_users.collect do |user|
        #
        # We pass the user to the thread, this is good
        # habit for shared variables, in this case
        # it doesn't make much difference
        #
        Thread.new(user) do |u|
          #
          # Do the API call here use `u` (not `user`)
          # to access the user instance
          #
          # We shouldn't need to use an evented HTTP library
          # Ruby threads will pass control when the IO happens
          # control will return to the thread sometime when
          # the scheduler decides, but 99% of the time
          # HTTP and network IO are the best thread optimized
          # thing you can do in Ruby.
          #
        end
      end
      #
      # Joining threads means waiting for them to finish
      # before moving onto the next batch.
      #
      batch_threads.map(&:join)
    end
    

    This will start no more than batch_size of threads, waiting after each batch_size to finish.

    It would be possible to do something like this, but then you will have an uncontrollable number of threads, there’s an alternative you might benefit from here, it gets a lot more complicated including a ThreadPool, and shared list of work to do, I’ve posted it as at Github so’as not to spam stackoverflow: https://gist.github.com/6767fbad1f0a66fa90ac

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string

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.