简体   繁体   English

我应该在一个类中封装Task吗?

[英]Should I encapsulate Task in a Class?

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 . 每个worker都是一个名为Bot的类中的方法。 Start/Stop of the Bot is controlled by the class itself. Bot启动/停止由类本身控制。 So if I need 10 bots running, I just need to instantiate 10 Bot , store in a List<Bot> , and start everyone of them. 因此,如果我需要运行10个机器人,我只需要实例化10个Bot ,存储在List<Bot> ,然后启动它们中的每个人。 I also have a BotManager class to manage all the currently running Bot 我还有一个BotManager类来管理所有当前运行的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. 现在,我想为每个工作人员使用Task而不是Thread ,因为他们只花费不到5%的时间进行任何处理。

Should I keep a Task in each class or should I inherit my Bot from Task ? 我应该在每个班级中保留一个Task ,还是应该从Task继承我的Bot

I wouldn't descend from Task . 我不会从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. 我也不确定Task是否适合这里。 If the idea is that these keep running in the background, then that's not playing to TPL's strengths. 如果想法是这些继续在后台运行,那么这不会影响TPL的优势。 The best time to use Task 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. 使用Task的最佳时机是当您需要可组合性时 - 一个定义明确的任务,运行,完成,然后通知其他任务已完成,并且他们可以开始处理其结果。

As I understood, your requirement is to run as many thread as possible to optimize the CPU usages. 据我所知,您的要求是尽可能多地运行线程以优化CPU使用率。 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. 同时你希望框架负责根据CPU使用情况来增加和减少当前运行的线程。

I would say TPL (Tasks) would the better option. 我会说TPL(任务)会更好。 Please look into http://msdn.microsoft.com/en-us/library/dd537609.aspx 请查看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? 24X7运行任务你到底意味着什么? What kind of work your tasks perform? 你的任务有什么样的工作? Is it computing or IO (DB operation/Web Service Call)? 是计算还是IO(数据库操作/ Web服务调用)?

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 我假设每个请求包含4个步骤,这些步骤按以下顺序执行:1。DB查找2. Web服务调用3.数据库更新4.登录到文件

As you mentioned Web service call takes time and therefore best suited for asynchronous I/O task. 正如您所提到的,Web服务调用需要时间,因此最适合异步I / O任务。

Next you said, 接下来你说,

CPU intensive part will be the db lookup CPU密集型部分将是数据库查找

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. 但我知道数据库查找不使用CPU,它是一个I / O活动,也是异步I / O任务的良好候选者。

Considering all these assumptions I suggest following, 考虑到我建议遵循的所有这些假设,

  1. DB Look-up (Run asynchronously so that thread pool thread will be freed to process other requests) 数据库查找(异步运行,以便释放线程池线程以处理其他请求)
  2. Web service call (Run asynchronously so that thread pool thread will be freed to process other requests) Web服务调用(异步运行,以便释放线程池线程以处理其他请求)
  3. DB Update (Run synchronously) 数据库更新(同步运行)
  4. Log to File (use asynchronous logging depending upon log content size) 记录到文件(根据日志内容大小使用异步日志记录)

According to my understanding none of your steps are CPU intensive that is why you are seeing only 5% of CPU usages. 根据我的理解,你的步骤都不是CPU密集型的,这就是为什么你只看到5%的CPU使用率。

You also mentioned, 你还提到过,

followed by a random delay to distribute the load evenly. 然后是随机延迟以均匀分配负载。

Did you mean to run as much as possible such requests (consists of these 4 steps)? 你的意思是尽可能多地运行这些请求(包括这4个步骤)? 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. 这将增加吞吐量。

Should I keep a Task in each class or should I inherit my Bot from Task? 我应该在每个班级中保留一个任务,还是应该从任务继承我的Bot?

1 Bot represent to 1 request. 1个Bot代表1个请求。 Tasks represent to Steps within a request. 任务代表请求中的步骤。 So create tasks within your BOT class. 因此,在BOT类中创建任务。

Run it with different number of requests and verify the usages of CPU, Memory and other system resources. 使用不同数量的请求运行它,并验证CPU,内存和其他系统资源的使用情况。

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. 如果要保存资源,请使用构造函数减少线程堆栈大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM