TPL 中的最大任务数?
我想在 Windows Azure 上的工作进程中使用 TPL。我希望在队列中添加一个 IJob,它有一个 Run 方法,因此工作线程将包括
: 将项目从队列中取出 使用 TPL 调用 IJob.Run,这是一个异步调用
,但我有点担心可以添加到 TPL 的最大项目数?如果需要的话,我很乐意构建自己的某种 TPL 池,只是检查它的功能。
干杯, 灰。
I want to use TPL in Worker process on Windows Azure. I'm looking to add an IJob the queue, this has a Run method, so the worker will consist of:
loop
get item off queue
Use TPL to call IJob.Run, this is an async call
But I'm a bit concerned about the maximum items I can add to TPL? I'm happy to build my own TPL Pool of some sort if required, just checking it capabilities.
Cheers,
Ash.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TPL 的主要目标之一是消除对此的担忧。通过将您的工作分解为任务而不是线程,您可以让调度程序更适当地处理此任务的平衡。
您可以安排的“任务”数量没有固定的上限。它们(默认情况下,使用默认的 TaskScheduler)使用 ThreadPool 进行调度,从 .NET 4 开始,ThreadPool 根据工作进行扩展。我强烈建议不要尝试建立自己的池 - 你不太可能比默认的池做得更好。话虽这么说,如果您的任务有非常不标准的行为,您可能需要考虑编写自定义 TaskScheduler。
另外 - 认识到理想情况下你应该让你的任务“尽可能大”。与单个任务相关的开销 - 让它们太小(就工作而言)将导致开销对性能产生比适当数量的较大“任务”更大的影响。
One of the main goals of the TPL is to remove the need to worry about this. By decomposing your work into Tasks instead of Threads, you're allowing the scheduler to handle the balancing of this more appropriately.
There is no fixed upper limit to the number of "tasks" you can schedule. They are (by default, with the default TaskScheduler) scheduled using the ThreadPool, which as of .NET 4, scales based on the work. I would strongly suggest not trying to build your own pool - it's highly unlikely that you'll do better than the default. That being said, if your tasks have a very non-standard behavior, you may want to consider writing a custom TaskScheduler.
Also - realize that you should, ideally, make your tasks as "large as possible". There is overhead associated with an individual task - having them be too small (in terms of work) will cause the overhead to have a larger impact on performance than if you have an appropriate number of larger "tasks".