Parallel.ForEachAsync 需要帮助

发布于 2025-01-20 04:46:39 字数 1215 浏览 4 评论 0原文

好的。因此,我将首先向前道歉,据我所知,关于并行和async有很多问题。但是,即使在搜索之后,我也无法围绕该如何工作。编码是我日复一日地做的事情。

我正在尝试帮助朋友使用Alphavantage API收集一些历史性的库存数据。

我在收集数据并将其保存到数据库时没有问题,但是要花20年的每日价格可能需要很长时间。因此,为了避免GUI冻结,我使其成为异步功能。但是,考虑到需要拉出多少数据,需要并行完成。我不确定有多少平行事件,但长期目标是在应用程序中将计时器置于时间间隔中,并调用batchupdateStockhistoryAsyAsyAsyAsyAsyAsync方法,并通过一个值来通过多少股票来拉动然后在此批处理中更新该功能,然后将出现,查询数据库并获取具有最古老更新的列表5。

目前,我将所有这些简单地剥离了这个问题,只是创建了5个迭代的手动列表。对于列表中的每个股票,它调用另一个函数PullandWriteHistoricDailyData(item),它可以实际联系到Alphavantage并更新DB。

在这个冗长的信息之后,我的问题是,最好的方法是触发pullandwritehistoricdailydataa()的多个并发线程?

我想我还需要使用maxdegreofparallelism来弄清楚什么最有效,但我甚至还没有走那么远。我相信我想使用Parallel.ForeachAsync?但是只是不确定如何将它们全部拉在一起。

任何帮助将不胜

感激

    public async Task BatchUpdateStockHistoryAsync()
    {
        List<string> list = new List<string>();

        list.Add("AAPL");
        list.Add("F");
        list.Add("MSFT");
        list.Add("COKE");
        list.Add("IAG");

        foreach(string item in list)
        {
            await Task.Run(() => PullAndWriteHistoricDailyData(item));
        }

        return completed;
    }

Ok. So I am going to start by apologizing up front as I know there have been a lot of question asked about Parallel and Async. However, even after searching I can not wrap my brain around how this should work. Coding is something I dabble in not something I do day in and day out.

I am trying to help a friend collect some historic stock data using the AlphaVantage API.

I have no issues collecting the data and saving it to the database but it can take a long time to pull 20 years of daily prices. So to avoid the GUI freezing up I made it an async function. However, Considering how often and how much data needs to be pulled it will need to be done in parallel. I am not sure how many parallel events yet but the long term goal is to have a timer in the application that fires off at intervals and calls the BatchUpdateStockHistoryAsync method and passes in a value for how many stocks to pull and update in this batch the function will then go out, query the DB and get a list 5 that have the oldest update.

For the moment I stripped all of that out to simply the question and just created a manual list of 5 stocks that get iterated through. For each stock in the list it calls another function PullAndWriteHistoricDailyData(item) that does all the work of actually reaching out to AlphaVantage and updating the DB.

After this lengthy info my question is what's the best way is to trigger multiple concurrent threads of PullAndWriteHistoricDailyData()?

I imagine I will need to also play with MaxDegreeOfParallelism to figure out what works best but I have not even gotten that far yet. I believe I would want to use Parallel.ForEachAsync maybe? but just not sure how to pull it all together.

Any help would be greatly appreciated

Misiu

    public async Task BatchUpdateStockHistoryAsync()
    {
        List<string> list = new List<string>();

        list.Add("AAPL");
        list.Add("F");
        list.Add("MSFT");
        list.Add("COKE");
        list.Add("IAG");

        foreach(string item in list)
        {
            await Task.Run(() => PullAndWriteHistoricDailyData(item));
        }

        return completed;
    }

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

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

发布评论

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

评论(1

深居我梦 2025-01-27 04:46:39

编辑我最初错误地阅读了 Task.WhenAllParallel.ForEach 的内容。根据 Theodore Zoulias 的回答阅读有关 Parallel.ForEachAsync 的更多信息后:is-parallel-foreachasync- a-replacement-to-a-plain-for-loop-append-to-task-list,并基于您想要查看每个任务的结果的假设要通过调用 Parallel.ForEachAsync 接收单个任务,那么我建议更改为使用以下内容:

您可以切换到使用 List 并使用 < code>await Task.WhenAll() 并跳过 Parallel.ForEachAsync,如下所示:

 List<Task> myupdatetasks = new List<Task>();

 foreach(string item in list)
 {
     myupdatetasks.Add(Task.Run(() => PullAndWriteHistoricDailyData(item)));
 }

 await Task.WhenAll(myupdatetasks).ConfigureAwait(false);

如果 PullAndWriteHistoricDailyData() 已经是async 方法,您不需要使用 Task.Run

 List<Task> myupdatetasks = new List<Task>();

 foreach(string item in list)
 {
     myupdatetasks.Add(PullAndWriteHistoricDailyData(item));
 }

 await Task.WhenAll(myupdatetasks).ConfigureAwait(false);

如果您是 ,您可以执行与上面相同的操作,减去 Task.Run打算使用 Parallel.ForEachAsync,这篇文章很好地解释了它:parallelforeachasync-in-net-6

EDIT I mistakenly read about Task.WhenAll vs Parallel.ForEach originally. After reading more about Parallel.ForEachAsync based on the answer by Theodore Zoulias at: is-parallel-foreachasync-a-replacement-to-a-plain-for-loop-append-to-task-list, and based on the assumption that you want to view the result of each Task as apposed to receiving a single Task from the call to Parallel.ForEachAsync, then I would propose changing to using the following:

You could switch to using a List<Task> and use await Task.WhenAll() and skip the Parallel.ForEachAsync like so:

 List<Task> myupdatetasks = new List<Task>();

 foreach(string item in list)
 {
     myupdatetasks.Add(Task.Run(() => PullAndWriteHistoricDailyData(item)));
 }

 await Task.WhenAll(myupdatetasks).ConfigureAwait(false);

if PullAndWriteHistoricDailyData() is already an async method, you don't need to use Task.Run, you could do the same as above minus the Task.Run

 List<Task> myupdatetasks = new List<Task>();

 foreach(string item in list)
 {
     myupdatetasks.Add(PullAndWriteHistoricDailyData(item));
 }

 await Task.WhenAll(myupdatetasks).ConfigureAwait(false);

if you are intent on using Parallel.ForEachAsync, this article explains it pretty well: parallelforeachasync-in-net-6

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