简体   繁体   English

这是Task Parallel Library的好用例吗?

[英]Is this a good use case for Task Parallel Library?

I have a situation and need some advice. 我有一种情况,需要一些建议。 I'm calling Twitter to get retweets, replies, likes, and stuff like that. 我打电话给Twitter以获得转发,回复,喜欢和类似的东西。 Twitter, of course, rates limit your calls so I need to pace out my calls. Twitter当然会限制您的通话费用,因此我需要加快通话速度。 And since I do not want to wait for a long time if one user is locked up because I still want to continue getting engagements for other users, I thought I should use multiple threads. 而且由于不想让一个用户被锁定而等待很长时间,因为我仍然想继续与其他用户互动,所以我认为我应该使用多个线程。 As an experiment, I tried Parallel task library something like: 作为实验,我尝试了并行任务库,例如:

Task.Factory.StartNew(() => Parallel.ForEach<BatchJob>(jobsToProcess, job =>
{
    //call Twitter here
}); 

From testing, it seems that this was eating up a lot of CPU and froze the whole system. 从测试来看,这似乎在消耗大量CPU并冻结了整个系统。 My question is: Is this a situation where using Parallel task will fit? 我的问题是:这是否适合使用并行任务? I do not need to wait for one task to finish to start another. 我不需要等待一项任务完成就可以开始另一项任务。 They all can run at the same time. 它们都可以同时运行。 In other words, the tasks do not depend on each other. 换句话说,任务不相互依赖。

If it is in a context of an webApp, your call will be lost. 如果在webApp的上下文中,您的呼叫将丢失。 Please view an session of build 2013 where they explain why. 请查看构建2013的会议,他们在其中解释原因。 If It is a desktop application, it should work as long as you use "dispatcher" object to update your visual. 如果它是桌面应用程序,则只要您使用“ dispatcher”对象来更新视觉效果,它就应该可以工作。

I don't like to use "task.factory", it's just me, but I prefer the syntax "new task (() => {...}) 我不喜欢使用“ task.factory”,而是我自己,但是我更喜欢使用语法“ new task(()=> {...})

Edit : no parallel task won't change a thing. 编辑:没有并行任务不会改变任何事情。

It's meant as comment, can't find a link for comment 它是评论,找不到评论链接

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

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