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 6851131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:12:37+00:00 2026-05-27T01:12:37+00:00

I am sending mail to the users using actionmailer through postmark. This is my

  • 0

I am sending mail to the users using actionmailer through postmark. This is my code in controller:

@users = User.where(some condition)
@product = Product.find_by_name(some name).first
for user in @users
  UserMailer.new_product_arrival(user, @product, home_url).deliver
end

and this my user_mailer.rb

def new_product_arrival(user,product,home_url)
  @from         = Settings.mailer_from_address
  @recipients   = user.login
  @sent_on      = Time.now
  @user = user
  @product = product
  @content_type = "text/html"
  @home_url = home_url
end

The problem is that if there are more than 10 users it takes a very long time because of the for loop. I need to know if we can handle this by using multi-threading or background job. I don’t want to use background job, but can anyone tell me how to implement the above using multi-threading.

I am using ruby 1.8.7 and rails 3.0.7

  • 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-27T01:12:38+00:00Added an answer on May 27, 2026 at 1:12 am

    There basically two ways to wrap your loop in order to get “multi-threading”:

    1. Spwan a thread for each delivery and join them back to the main thread

      threads = []
      for user in @users
         threads << Thread.new do
           UserMailer.new_product_arrival(user, @product, home_url).deliver
         end
      end
      threads.each(&:join)
      
    2. fork over the entire rails app ( pretty messy but the rails app serving the request will respond immediately ) and have the process detached:

      process = fork do
        for user in @users
          UserMailer.new_product_arrival(user, @product, home_url).deliver
        end
        Process.kill("HUP") 
        #sends the kill signal to current Process, which is the Rails App sending your emails 
      end
      Process.detach(process)
      

    Hope that helps

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

Sidebar

Related Questions

I am sending emails to users using Django through Google Apps. When the user
Using the obsolete System.Web.Mail sending email works fine, here's the code snippet: Public Shared
I'm sending a simple mail with attachment using SmtpClient but I get this error:
sending mail along with embedded image using asp.net I have already used following but
I'm sending mail from my C# Application, using the SmtpClient. Works great, but I
Why does Thunderbird always return immediately after sending mail through MAPI? Other clients, like
Sending a mail through my windows application to a person asking him to log
Sending a message from the Unix command line using mail TO_ADDR results in an
I'm using PHPMailer for sending activation codes to users. As far as I know,
In my Project i am using YiiMail extension to send mail to the users.

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.