线程池和发送电子邮件 [英] ThreadPool and sending emails

查看:211
本文介绍了线程池和发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我们正在发送电子邮件给用户asynchronoulsy使用线程池。从本质上讲,我们有逻辑归结为:

We are currently sending emails to users asynchronoulsy using the ThreadPool. Essentially, we have logic that comes down to this:

for (int i=0 < i < numUsers; i++) 
{

   //Pre email processing unrelated to sending email

   string subject = GetSubject();
   string message = GetMessage();
   string emailAddress = GetEmailAddress();

   EmailObj emailObj = new EmailObj { subject = subject, message = message, emailAddress = emailAddress };

   bool sent = ThreadPool.QueueUserWorkItem(new WaitCallback(SendEmail), emailObj);

   //Post email processing unrelated to sending email
}

public void SendEmail(object emailObj)
{
    //Create SMTP client object
    SmtpClient client = new SmtpClient(Configuration.ConfigManager.SmtpServer);

    //Logic to set the subject, message etc
    client.Send(mail);
}

逻辑的伟大工程与用户的数量较少为止。我们正在努力扩展这是能够发送一百万左右的电子邮件。

The logic works great so far with a low number of users. We are trying to scale this to be able to send a million or so emails.

每MSDN,线程池的最大线程数是基于存储器和根据本 SO回答,对于64位架构,似乎线程池中的线程的最大数量为32768。

Per MSDN, the maximum number of thread pool threads is based on memory and according to this SO answer, for a 64 bit architecture, it appears that the maximum number of thread pool threads is 32768.

这是否意味着,只要我们发送的邮件数量在同一时间为&lt; 32768,我们应该可以了?当超过这个数字,会发生什么?当SMTP服务被关闭或者有一个在发送邮件的延迟会发生什么,将线程池中的线程等到发送电子邮件?

Does this mean, that as long as the number of emails we send out at a time is < 32768, we should be fine? What happens when this number is exceeded? What happens when the SMTP service is turned off or there's a delay in sending an email, will the thread pool thread wait until the email is sent?

当线程数超过阈值,并在部分标注//邮邮件processsing无关发送电子邮件获得所有执行?

When the number of threads exceed the threshold, does the section marked //Post email processsing unrelated to sending email get executed at all?

任何解释都是真的AP preciated。

Any explanations are really appreciated.

推荐答案

线程都开销 - 线程本地存储容量为1MB。你绝不会希望有32K的线程在你的线程池。线程池用于门和共享的线程,因为他们的开销。如果线程池被饱和,未来的呼叫排队,等待池中的可用线程。

Threads have overhead - 1MB of thread local storage. You would never want to have 32K threads in your thread pool. A thread pool is used to gate and share threads because they have overhead. If the thread pool gets saturated, future calls are queued and wait for an available thread in the pool.

另一个要考虑的是SMTP服务器是异步的(下降传出文件夹)。另外,如某人如上所述,它可以是一个瓶颈。

Another thing to consider is SMTP servers are asynchronous (drop in outbound folder). Also, as someone above mentioned, it can be a bottle neck.

一种选择是通过增加'代理'发送邮件的数量,以增加吞吐量和增加的SMTP服务器的数量,以向外扩展的解决方案。作为能够独立向外扩展的代理和SMTP服务器,您可以解决的瓶颈。

One option is to increase the throughput by increasing the number of 'agents' sending mails and increase the number of SMTP servers to scale out the solution. Being able to independently scale out the agents and the SMTP servers allows you to address the bottleneck.

这篇关于线程池和发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆