取消 PLINQ 中长时间运行的任务

发布于 2024-09-10 16:36:35 字数 964 浏览 5 评论 0原文

我正在尝试使用 NET 4.0 并行任务库来处理多个 FTS 查询。如果查询花费太多时间,我想取消它并继续处理其余部分。

当一个查询超过阈值时,此代码不会停止。我想我这样称呼它是为了整个过程而不是单个事务达到取消任务和时间限制。如果我将时间段设置得非常小(300 毫秒),那么所有搜索字符串都会调用它。

我想我错过了一些明显的东西..提前感谢您的任何见解。

此外,这似乎仍然无法阻止很长的查询的执行。这是否是触发长时间运行的查询后取消该查询的正确方法?

修改后的代码:

CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken token = cts.Token;

var query = searchString.Values.Select(c =>myLongQuery(c)).AsParallel().AsOrdered()
                                        .Skip(counter * numToProcess).Take(numToProcess).WithCancellation(cts.Token);

  new Thread(() =>
  {
     Thread.Sleep(5000);
     cts.Cancel();
  }).Start();

  try
  {
     List<List<Threads>> results = query.ToList();
     foreach (List<Threads> threads in results)
     {
           // does something with data
     }
  } catch (OperationCanceledException) {
     Console.WriteLine("query took too long");
  }   

I am trying to use the NET 4.0 parallel task library to handle multiple FTS queries. If the query takes too much time, I want to cancel it and continue forward with processing the rest.

This code doesn't stop when one query goes over the threshold. I think I'm calling it such that the cancel task and time limit is reached for the whole of the process rather than the single transaction. If I set the time period to be very small (300ms), then it gets called for all search strings.

I think I'm missing something obvious .. thanks in advance for any insight.

Additionally, this still doesn't seem to stop the very long query from executing. Is this even the correct way to cancel a long running query once it's been triggered?

Modified code:

CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken token = cts.Token;

var query = searchString.Values.Select(c =>myLongQuery(c)).AsParallel().AsOrdered()
                                        .Skip(counter * numToProcess).Take(numToProcess).WithCancellation(cts.Token);

  new Thread(() =>
  {
     Thread.Sleep(5000);
     cts.Cancel();
  }).Start();

  try
  {
     List<List<Threads>> results = query.ToList();
     foreach (List<Threads> threads in results)
     {
           // does something with data
     }
  } catch (OperationCanceledException) {
     Console.WriteLine("query took too long");
  }   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几度春秋 2024-09-17 16:36:35

PLINQ 将在每隔一定数量的元素后轮询取消标记。如果检查频率不足以满足您的应用程序的需要,请确保 PLINQ 查询中所有昂贵的委托定期调用 cts.Token.ThrowIfCancellationRequested()。

有关更多详细信息,请参阅本文:链接

PLINQ will poll the cancellation token after every some number of elements. If the frequency of checks is insufficient for your application, make sure all expensive delegates in the PLINQ query regularly call cts.Token.ThrowIfCancellationRequested().

For more details, see this article: Link

贱贱哒 2024-09-17 16:36:35

这只是一个猜测:问题是不是查询是惰性的(如在普通 LINQ 中),因此直到稍后才执行?

This is just a guess: isn't the problem that the query is lazy (as in normal LINQ) and so it isn't executed until later?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文