.NET 中线程池排队的最佳项目数?
我正在构建一个后台处理引擎,它支持丢弃待处理和正在处理的项目。这是用于需要大量处理某些输入元素的 winforms 应用程序,因此我正在构建一个队列引擎,可以将工作负载项目排入队列,并且在处理它们时,我会收到结果通知。
问题是,这个队列几乎总是包含很多项目,我认为,我不只是将所有内容转储到线程池中,而是只将前 N 个项目放入线程池中,并在它们出现时继续回填。已处理。我想这样做的原因是,一旦我将它们转储到线程池中,它们就会被处理,即使它们被标记为丢弃,它们仍然会占用队列时间。
通过我所做的回填实现,如果项目被丢弃,我可以将其从队列中删除,并且只有在轮到它们时才将它们放入队列中,可以这么说。
所以问题是,我将如何计算这个数字 N,即要放入并保留在线程池队列中的项目数。
我考虑过的问题:
- 我可能想要排队 2 * 数量的处理器(我认为这是典型的项目数量),以确保所有处理器都在工作
- 但是,如果某些项目的实际处理速度超快(这可能会发生) ),那么在我自己的类可以回填更多工作之前,线程池中的队列就已耗尽,所以也许我想要一个更大的数字,以避免处理器利用率不足。
- 我应该创建一些自动调整例程来根据每个项目所花费的当前时间,因此,如果它们都超快,则数字会高得多,如果处理需要一点时间,则应该保持较低水平?
你怎么认为?
新:好的,由于其中一个答案,我将解释更多一点。放入队列中的每个项目都有一个独特的键。如果我将另一个项目与现有项目具有相同的键转储到队列中,则该旧项目将被视为“丢弃”,并且应该被删除。如果正在处理该项目,则工作负载项目上的属性设置为 true,即“IsDicarded”属性,处理方法负责调用该属性。如果它检测到丢弃的物品,它应该提前退出,不返回任何结果。
也许我应该多做一些实验,并尝试将所有内容转储到线程池中。
新问题:我可以排队的商品数量有限制吗?如果没有,那么这将很容易地简化我的课程。
注意:当我说“长时间处理”时,我的意思是 1-10 秒左右。线程池是最适合这个的吗?我在网上看到很多关于“处理应该很快”的注释,但是从来没有提到什么是“快速”。这里的速度是毫秒级的吗?
I am building a background processing engine which supports discarding both to-be-processed and is-being-processed items. This is for usage in a winforms application that will require heavy processing of some input elements, so I'm building a queue engine where I can enqueue workload items, and when they're processed, I get notified with the results.
The question is, this queue will almost always contain a lot of items to begin with, and I thought that instead of just dumping everything to the threadpool, I'd place only the first N items into the threadpool, and keep backfilling when they are processed. The reason I want to do this is that once I dump them into the threadpool, they will be processed, and even if they're tagged as discard, they will still take up queue time.
With the backfill implementation I've made, I can remove items from the queue if they become discarded, and only put them into the queue when it's their turn, so to speak.
So the question is, how would I go about calculating this number N, the number of items to place into and keep in the thread pool queue.
Issues I've considered:
- I might want to enqueue 2 * number of processors, which I see is a typical number of items, to ensure all processors are working
- However, if the actual processing of some items is super-fast (which can happen), then the queue in the threadpool is exhausted before my own class can backfill with more work, so perhaps I'd like a bigger number to avoid underutilizing the processors
- Should I create some auto-adjust routine to calculate the optimal number based on the current time each item takes, so that if they are all super-fast, the number is much higher, and if processing takes a bit of time, it should stay low?
What do you think?
New: Ok, due to one of the answers, I'll explain a bit more. Every item put into the queue is keyed by something unique. If I dump another item into the queue with the same key as an existing item, that old item is considered "Discarded", and should be removed. If the item is being processed, a property on the workload item is set to true, a "IsDicarded" property, which the processing method is responsible for calling. If it detects a discarded item, it should quit early, returning no results.
Perhaps I should experiment a bit more, and try to just dump everything into the threadpool.
New question: Is there a limit to the number of items I can queue up? If not, then this would easily simplify my class a lot.
Note: When I say "lengthy processing", I mean in the order of 1-10 seconds. Is the threadpool even the best for this? I see notes all over the web about "the processing should be quick", but what "quick" is is never mentioned. Is quick in the order of milliseconds here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你知道Ami Bar的智能线程池吗?
似乎它的实现允许您取消未处理的项目并根据需要动态增加线程,直到硬限制;我个人使用
100 * Environment.ProcessorsCount
Do you know Ami Bar's Smart Thread Pool?
Seems its implementation allows you to cancel an unprocessed item and dynamically increases threads as required, until a hard limit; I personally use
100 * Environment.ProcessorsCount
您是否可以通过修改您的项目来简化该方法,以在进行任何工作之前首先检查它们是否仍然需要?这将解决限制池中数量的问题,因为您可以简单地将它们全部添加,并且当每个项目被处理时,如果不再需要,它将退出。
http://msdn.microsoft.com/en-us/library/0ka9477y。 ASPX
Is it possible you could simplify the approach by modifying your items to first check they are still required before they do any work? This would skirt the problem of limiting the number in the pool, since you can simply add them all and when each item gets processed it will exit if no longer needed.
http://msdn.microsoft.com/en-us/library/0ka9477y.aspx