我应该将任务封装在一个类中吗?
我正在构建一个具有多个长时间运行的工作线程的应用程序,这些线程需要 24x7 运行。每个工作人员都是名为 Bot
的类中的一个方法。 Bot
的启动/停止由类本身控制。因此,如果我需要运行 10 个机器人,我只需要实例化 10 个 Bot
,存储在 List
中,然后启动每个机器人。我还有一个 BotManager
类来管理所有当前运行的 Bot
现在,我想使用 Task
而不是 Thread
> 对于每个工人来说,因为他们只花费不到 5% 的时间进行任何处理。
我应该在每个类中保留一个 Task
还是应该从 Task
继承我的 Bot
?
I'm building an application having multiple long running worker threads which need to be running 24x7. Each worker is a method in a class called Bot
. Start/Stop of the Bot
is controlled by the class itself. So if I need 10 bots running, I just need to instantiate 10 Bot
, store in a List<Bot>
, and start everyone of them. I also have a BotManager
class to manage all the currently running Bot
Now, I wanted to use Task
instead of Thread
for each of the workers, as they're only spending less than 5% of the time doing any processing.
Should I keep a Task
in each class or should I inherit my Bot
from Task
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不会从
Task
下降。您并没有真正扩展“任务”的概念,并制作一些对于使用它的代码来说是具有额外功能的任务;相反,您只是在制作一些碰巧使用任务来实现的东西。我也不确定
Task
是否适合这里。如果这些想法一直在后台运行,那么就无法发挥 TPL 的优势。使用Task
的最佳时机是当您需要可组合性时 - 一个定义明确的任务,可以运行、完成,然后通知其他任务它已完成并且他们可以开始处理其结果。I wouldn't descend from
Task
. You're not really extending the idea of "task" and making something that, to code that uses it, is a task with extra features; instead you're just making something that happens to use tasks for its implementation.I'm also not sure
Task
is a good fit here. If the idea is that these keep running in the background, then that's not playing to TPL's strengths. The best time to useTask
is when you need composability -- a well-defined task that runs, completes, and then notifies other tasks that it's done and they can start working on its results.据我了解,您的要求是运行尽可能多的线程以优化 CPU 使用率。同时您希望框架负责根据 CPU 使用情况增加和减少当前运行的线程。
我想说 TPL(任务)是更好的选择。请查看 http://msdn.microsoft.com/en-us/library/ dd537609.aspx
如果您分享您想要并行执行的工作的性质,我们可以为您提供更多帮助。 7天24小时运行任务到底是什么意思?您的任务执行什么类型的工作?是计算还是IO(DB操作/Web服务调用)?
我假设每个请求由 4 个步骤组成,这些步骤按以下顺序执行,
1.数据库查找
2.Web服务调用
3.数据库更新
4. 记录到文件
正如您所提到的,Web 服务调用需要时间,因此最适合异步 I/O 任务。
接下来你说,
但我知道数据库查找不使用 CPU,它是一项 I/O 活动,也是异步 I/O 任务的良好候选者。
考虑到所有这些假设,我建议遵循以下步骤:
根据我的理解,您的所有步骤都不是 CPU 密集型的,这就是为什么您只看到 5% 的 CPU 使用率。
您还提到,
您是否打算尽可能多地运行此类请求(由这 4 个步骤组成)?如果是这样,那么您不需要放置任何额外的东西。因为当请求等待异步调用时,它会释放线程池线程,系统可以使用它来处理其他请求。这将增加吞吐量。
1 个机器人代表 1 个请求。任务代表请求中的步骤。因此,请在 BOT 类中创建任务。
使用不同数量的请求运行它并验证 CPU、内存和其他系统资源的使用情况。
As I understood, your requirement is to run as many thread as possible to optimize the CPU usages. And the same time you want framework to take care of this responsibility to increase and decrease the current running threads according to CPU usage.
I would say TPL (Tasks) would the better option. Please look into http://msdn.microsoft.com/en-us/library/dd537609.aspx
We can help you more if you share the nature of your work you want to perform in parallel. What do you exactly mean by 24X7 running tasks? What kind of work your tasks perform? Is it computing or IO (DB operation/Web Service Call)?
I am assuming that each request consists of 4 steps and these steps are executed in following sequence,
1. DB Look-up
2. Web service call
3. DB Update
4. Log to File
As you mentioned Web service call takes time and therefore best suited for asynchronous I/O task.
Next you said,
But I understand that db lookup does not use CPU and it is an I/O activity and again good candidate for asynchronous I/O task.
Considering all these assumptions I suggest following,
According to my understanding none of your steps are CPU intensive that is why you are seeing only 5% of CPU usages.
You also mentioned,
Did you mean to run as much as possible such requests (consists of these 4 steps)? If so then you do not need to put any extra stuff. Because when a request waits for asynchronous call then it frees the thread pool thread and system can use it to process other request. This will increase the throughput.
1 Bot represent to 1 request. Tasks represent to Steps within a request. So create tasks within your BOT class.
Run it with different number of requests and verify the usages of CPU, Memory and other system resources.
这绝对不是任务的“任务”。使用线程。当您需要确保您的代码确实正在运行(并且在固定数量的线程上)时,您无法使用任务。任务不保证并行执行。您必须自己分配线程。如果要节省资源,请使用构造函数减少线程堆栈大小。
This is definitely not a "task" for tasks. Use threads. When you need assurance, that your code is actually running (and on a fixed amount of threads) you cannot use tasks. Tasks do not guarantee parallel execution. You must allocate threads yourself. If you want to save resources reduce the threads stack size using the constructor.